简体   繁体   中英

Convert Json string to Java object gives null (null)

Hello i got this Json string

{"NexusResource":{"resourceURI":"http://nexus.ad.hrm.se/nexus/service/local/repositories/snapshots/content/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/","relativePath":"/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/","text":"16.1-SNAPSHOT","leaf":false,"lastModified":"2018-04-09 12:23:59.0 UTC","sizeOnDisk":-1}}

I want to convert this to an object of a class named NexusResource that looks like this

public class NexusResource {
@JsonProperty("resourceURI") private String resourceURI;
@JsonProperty("relativePath") private String relativePath;
@JsonProperty("text") private String text;
@JsonProperty("leaf") private Boolean leaf;
@JsonProperty("lastModified") private String lastModified;
@JsonProperty("sizeOnDisk") private Integer sizeOnDisk;
@JsonIgnore private Map<String, Object> additionalProperties = new HashMap<>();
}

i try to convert it with an ObjectMapper

 ObjectMapper mapper = new ObjectMapper();
    NexusResource resource = mapper.readValue(version, NexusResource.class);

were version is the Json string but when i log resource all i get is null (null) even though version got all the data.

You can configure your ObjectMapper to unwrap the root value, in order to de-serialize into your POJO.

Eg:

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

See API .

You could also work around that by modifying your POJO (see Karol 's answer).

Failure to choose either should result in a com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException being thrown, with message: Unrecognized field "NexusResource" .

NexusResource is not a root of your JSON but a key. To make your Java mapping work you should define a wrapping type:

public class NexusResources {
  @JsonProperty("NexusResource") private NexusResource root;
  ...
}

and then use it to map:

ObjectMapper mapper = new ObjectMapper();
NexusResources root = mapper.readValue(version, NexusResources.class);
NexusResource resource = root.getRoot();

The problem is that the JSON does not match the class you are trying to parse. Please notice that the JSON has a field called "NexusResource" that has all the other fields. Whereas the class NexusResource.class just has the fields. Two things you can do. Change the JSON to match NexusResource.class, or create a new class that matches the JSON.

1) Change the json to the following.

{"resourceURI":"http://nexus.ad.hrm.se/nexus/service/local/repositories/snapshots/content/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/","relativePath":"/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/","text":"16.1-SNAPSHOT","leaf":false,"lastModified":"2018-04-09 12:23:59.0 UTC","sizeOnDisk":-1}

2) Create new class that actually matches your Json.

class NexusResourceJson {

    @JsonProperty("NexusResource ")
    NexusResource resource;

    public NexusResource getResource() {return resource;}
}


ObjectMapper mapper = new ObjectMapper();
NexusResource resource = mapper.readValue(version, NexusResourceJson.class).getResource();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM