如何使用 GSON 正确解析这个 json 字符串。 JsonObject 返回空,没有解析错误
我的数据结构如下
{"testString":"some string","success":true,"reason":null,"data":{"networks":[{"networkId":"1","networkName ":"some area","networkType":1},{"networkId":"4","networkName":"another place","networkType":1}]}}
所以基本上它是一个数组对象,第一个你键/值对是 1)String 2)Bool 3)String,第四个是 JSONObject
我用作所有这些数据的容器的对象是
public class ContainerData {
private boolean success;
private String reason;
private JSONObject data;
private String testString;
public String getTestString(){
return this.testString;
}
public void setTestString(String test){
this.testString = test;
}
public boolean getSuccess() {
return this.success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getReason() {
return this.reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public JSONObject getData() {
return this.data;
}
public void setData(JSONObject data) {
this.data = data;
}
}
我像这样解析它,响应对象是
Gson gson = new GsonBuilder().serializeNulls().create();
responseObject = gson.fromJson(response,ContainerData.class);
我可以获得的 json 字符串除了 JSONObject 之外的所有值,它总是返回一个空数组,例如 {} 对我搞砸的地方有什么想法吗?
I have a data structure as follows
{"testString":"some string","success":true,"reason":null,"data":{"networks":[{"networkId":"1","networkName":"some area","networkType":1},{"networkId":"4","networkName":"another place","networkType":1}]}}
So basically it's an array object with the first thee key/value pairs being a 1)String 2)Bool 3)String and the fourth is a JSONObject
The Object i'm using as a container for all this data is
public class ContainerData {
private boolean success;
private String reason;
private JSONObject data;
private String testString;
public String getTestString(){
return this.testString;
}
public void setTestString(String test){
this.testString = test;
}
public boolean getSuccess() {
return this.success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getReason() {
return this.reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public JSONObject getData() {
return this.data;
}
public void setData(JSONObject data) {
this.data = data;
}
}
I parse it like this withr esponse object being the json string
Gson gson = new GsonBuilder().serializeNulls().create();
responseObject = gson.fromJson(response,ContainerData.class);
I can get values for everything except the JSONObject it always returns an empty array like {} Any thoughts on where I'm messing up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种使用您提供的 JSON 输入的方法,但更“完全”反序列化为友好的数据结构。
输出是
Here's an approach that works with the JSON input you provided, but more "fully" deserializes into a friendly data structure.
The output is
我已经解析了你上面提到的 JSON 字符串。它没有使用 GSON 来解析 JSON。请也尝试一下这个。我认为这会解决你的问题:
I have parsed the JSON string you have mentioned above. It is not using the GSON to parse the JSON. Please try this also. I think it will sove your issue: