像JSON字符串一样的解析地图撰写的Fastjson/Jackson
考虑一个JSON:
{
"data": {
"key1": {
"t1":"text1",
"t2":"text1",
},
"key2": {
"t1":"text1",
"t2":"text1",
},
...
}
"code": 1
}
使用数据Java类定义如下:
@Getter
@Setter
public class JsonData {
private Data data;
private int code;
}
@Getter
@Setter
public class Data {
private Map<String, KeyInfo> mapKeyInfo;
}
@Getter
@Setter
public class KeyInfo {
private String t1;
private String t2;
}
我尝试获取包含JSON内容但NullPoInterException的JSondata对象:
JsonData jsonData = JSONObject.parseObject(response, JsonData.class); // NullPointerException
如果我想以FastJSON的方式进行该怎么办?如果我用杰克逊做同样的事情怎么办?
更新
我尝试了一个课程来解析,但仍然失败:
@Getter
@Setter
public class JsonData {
private Map<String, Map<String, String>> data;
private int code;
}
Consider a json:
{
"data": {
"key1": {
"t1":"text1",
"t2":"text1",
},
"key2": {
"t1":"text1",
"t2":"text1",
},
...
}
"code": 1
}
With data java class defined as below:
@Getter
@Setter
public class JsonData {
private Data data;
private int code;
}
@Getter
@Setter
public class Data {
private Map<String, KeyInfo> mapKeyInfo;
}
@Getter
@Setter
public class KeyInfo {
private String t1;
private String t2;
}
I try to get the JsonData object containing the json content but NullPointerException:
JsonData jsonData = JSONObject.parseObject(response, JsonData.class); // NullPointerException
What should I do if I want to do in fastjson way? What if I use jackson to do the same thing?
Update
I tried one class to parse but still failed:
@Getter
@Setter
public class JsonData {
private Map<String, Map<String, String>> data;
private int code;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论