无法使用jackson lib解析java中JSON.stringify的nodejs输出

时间:2016-02-11 07:21:29

标签: java json node.js

下面的字符串是我从节点js获得的输出,它使用json.stringify方法。此字符串不是正确的java字符串,因此不可解析。如何在java中解析它?我正在使用Jackson lib来支持Json。

{"m":"data","dID":"11:22:33:44:55:66","t":"Thu, 11 Feb 2016 05:34:52 GMT","d":[{"sID":"hydrogen","d1":44.27927702199668,"d2":0.26740958681330085,"d3":10.4},{"sID":"gps","d1":-9.007042,"d2":53.292506,"d3":63}]}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

首先创建您的POJO:

public class MyFirstClass {

    private String m;
    private String dID;
    private String t;
    private List<MySecondClass>;

    //getters and setters

}

public class MySecondClass {

    private String sID;
    private double d1;
    private double d2;
    private double d3;

    //getters and setters  

}

然后反序列化你的json:

ObjectMapper mapper = new ObjectMapper();
MyFirstClass response = mapper.readValue(jsonStringResponse, MyFirstClass.class);

希望它有所帮助。

答案 1 :(得分:0)

你已经拥有了jackson,只需创建POJO并将其成员注释为json字符串,如下所示

class C{
    @JsonProperty("m")
    private String m; 
    /*getter, setter */
}