1

I am trying to map this JSONArray using Spring RestTemplate:

[{
    "Command": "/usr/sbin/sshd -D",
    "Created": 1454501297,
    "Id": "e00ca61f134090da461a3f39d47fc0cbeda77fbbc0610439d3c16a932686b612",
    "Image": "ubuntu:latest",
    "Labels": {

    },
    "Names": [
        "/nova-c1896fbd-1309-4da2-8d77-b4fe4c02fa8e"
    ],
    "Ports": [

    ],
    "Status": "Up 2 hours"
}, {
    "Command": "/usr/sbin/sshd -D",
    "Created": 1450106126,
    "Id": "7ffc9dbdd200e2c23adec442abd656ed57306955332697cb7da979f36ebf3b22",
    "Image": "ubuntu:latest",
    "Labels": {

    },
    "Names": [
        "/nova-93b9ae40-8135-48b7-ac17-12094603b28c"
    ],
    "Ports": [

    ],
    "Status": "Up 2 hours"
}]

Here is ContainersInfo class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ContainersInfo {


    private String Id;


    private List<String> Names;

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public List<String> getNames() {
        return Names;
    }

    public void setNames(List<String> names) {
        Names = names;
    }
}

However I get null when I want to get the data:

ContainersInfo[] containers = syncRestTemplate.getForObject("http://192.168.1.2:4243/containers/json?all=1", ContainersInfo[].class);

for (int i = 0; i < containers.length; i++)
            System.out.println("id:" + containers[i].getId());

The resulting output is as follows:

id:null

id:null

Any idea, what I should do?

4
  • Are you sure your Labels part is object? Or it should be array? Commented Feb 9, 2016 at 13:10
  • I even don't try to map Labels I just try to get Id and Names in ContainersInfo class Commented Feb 9, 2016 at 13:11
  • Try removing Labels . Commented Feb 9, 2016 at 13:13
  • @PradipBorde I can't do that, that's the JSON I receive from a server. Commented Feb 9, 2016 at 13:14

1 Answer 1

1

Your JSON field names are in pascal case as opposed to camel case (which is usually the case). Set Jackson naming strategy to PascalCaseStrategy, i.e by adding @JsonNaming(PascalCaseStrategy.class) annotation into ContainersInfo class.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah I know chrome extension to show JSON data in formatted way, changes that. That's not the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.