使用 jackson Parser 解析 Json 字符串

nad87563

我能够解析下面的json:

{
    "jobId": "xxx",
    "jobName": "xxx",
    "jobInput": "xxx"
}

final ObjectMapper mapper = new ObjectMapper();
Map<?, ?> map = mapper.readValue(jsonString, Map.class);

我需要在java中使用jackson解析器解析下面的json字符串。

{
"Test1": {
    "jobId": "xxx",
    "jobName": "xxx",
    "jobInput": "xxx"
  },
"Test2": {
    "jobId": "xxx",
    "jobName": "xxx",
    "jobInput": "xxx"
  }
}
卡西莫林

使用 Jackson,您可以执行以下操作:

ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {};
Map<String, Object> data = mapper.readValue(json, typeRef);

如果您更喜欢使用自定义类来保存值而不是 a Map,请使用:

ObjectMapper mapper = new ObjectMapper();
Data data = mapper.readValue(json, Data.class);
public class Data {

    @JsonProperty("Test1")
    private Job test1;

    @JsonProperty("Test2")
    private Job test2;

    // Default constructor, getters and setters
}
public class Job {

    private String jobId;

    private String jobName;

    private String jobInput;

    // Default constructor, getters and setters
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章