简体   繁体   English

将Json字符串转换为Java对象会给出null(null)

[英]Convert Json string to Java object gives null (null)

Hello i got this Json string 您好,我得到了这个Json字串

{"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 我想将其转换为名为NexusResource的类的对象,如下所示

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进行转换

 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. 版本是Json字符串,但是当我记录资源时,即使版本获取了所有数据,我得到的还是null(空)。

You can configure your ObjectMapper to unwrap the root value, in order to de-serialize into your POJO. 您可以将ObjectMapper配置为解包根值,以便反序列化到POJO中。

Eg: 例如:

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

See API . 参见API

You could also work around that by modifying your POJO (see Karol 's answer). 您也可以通过修改POJO来解决此问题(请参阅Karol的答案)。

Failure to choose either should result in a com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException being thrown, with message: Unrecognized field "NexusResource" . 不选择其中任何一个都将导致com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException抛出,并显示消息: Unrecognized field "NexusResource"

NexusResource is not a root of your JSON but a key. NexusResource不是JSON的根,而是密钥。 To make your Java mapping work you should define a wrapping type: 为了使Java映射正常工作,您应该定义包装类型:

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. 问题是JSON与您尝试解析的类不匹配。 Please notice that the JSON has a field called "NexusResource" that has all the other fields. 请注意,JSON有一个名为“ NexusResource”的字段,其中包含所有其他字段。 Whereas the class NexusResource.class just has the fields. NexusResource.class类仅具有字段。 Two things you can do. 您可以做两件事。 Change the JSON to match NexusResource.class, or create a new class that matches the JSON. 更改JSON以匹配NexusResource.class,或创建一个与JSON匹配的新类。

1) Change the json to the following. 1)将json更改为以下内容。

{"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. 2)创建与您的Json实际匹配的新类。

class NexusResourceJson {

    @JsonProperty("NexusResource ")
    NexusResource resource;

    public NexusResource getResource() {return resource;}
}


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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