GSON中解析JSON应该是我的类结构 [英] What should be my class structure to parse JSON in GSON
本文介绍了GSON中解析JSON应该是我的类结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON数据:
{
response:{},
errorMessage:{
error:[
{
errorId:260003,
domain:ads,
subdomain: asd,
severity:asd,
category:asd,
message:asdsa asd ad,
errorName:未经授权的
}
]
}
}
目前我有以下类结构:
public class JSONCollection
private response response;
私人ErrorMessage错误;
public class Response
private String collectionId;
私人字符串网址;
public class ErrorMessage
private List< ErrorValues>错误;
public class ErrorValues
private String errorId;
私人字符串域;
私人字符串子域;
私人字符串严重性;
私有字符串类别;
私人字符串消息;
private String errorName;
我为所有私有变量设置了setters / get设置
但是,当我做一个 JSONCollection cJson = gson.fromJson(JSONValue,JSONCollection.class);
我得到cJson作为 null
。
如何正确使用?
解决方案
我使用@JigarJoshi显示的此工具来生成我的模式。
我发现的唯一区别是我必须将类名从 ErrorValues
更改为 Error
I have the following JSON data:
{
"response": {},
"errorMessage": {
"error": [
{
"errorId": 260003,
"domain": "ads",
"subdomain": "asd",
"severity": "asd",
"category": "asd",
"message": "asdsa asd ad",
"errorName": "UnAuthorized"
}
]
}
}
Currently I have the following class structure:
public class JSONCollection
private Response response;
private ErrorMessage error;
public class Response
private String collectionId;
private String url;
public class ErrorMessage
private List<ErrorValues> error;
public class ErrorValues
private String errorId;
private String domain;
private String subdomain;
private String severity;
private String category;
private String message;
private String errorName;
I have setters/get set for all private variables
But when I do a JSONCollection cJson = gson.fromJson(JSONValue,JSONCollection.class);
I get cJson as a null
.
How to get it right?
解决方案
I used this tool shown by @JigarJoshi to generate my schema.
The only difference I found is I had to change the class name from ErrorValues
to Error
这篇关于GSON中解析JSON应该是我的类结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文