我的数据结构如下
{“testString”:“some string”,“success”:true,“reason”:null,“data”:{“networks”:[{“networkId”:“1”,“networkName”:“some” area“,”networkType“:1},{”networkId“:”4“,”networkName“:”另一个地方“,”networkType“:1}]}}
所以基本上它是一个数组对象,第一个键/值对是1)字符串2)Bool 3)字符串,第四个是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;
}
}
我解析它就像这个withpon esponse对象是json字符串
Gson gson = new GsonBuilder().serializeNulls().create();
responseObject = gson.fromJson(response,ContainerData.class);
我可以获取除JSONObject之外的所有值的值,它总是返回一个像{}那样的空数组。对于我搞砸的地方有任何想法吗?
答案 0 :(得分:2)
这是一种与您提供的JSON输入一起使用的方法,但更加“完全”反序列化为友好的数据结构。
输出
[ContainerData: success=true, reason=null, testString=some string, data=[Data: networks=[ [Network: networkId=1, networkName=some area, networkType=1], [Network: networkId=4, networkName=another place, networkType=1] ]]]
public class Foo
{
static String jsonInput =
"{" +
"\"testString\":\"some string\"," +
"\"success\":true," +
"\"reason\":null," +
"\"data\":" +
"{" +
"\"networks\":" +
"[" +
"{\"networkId\":\"1\",\"networkName\":\"some area\",\"networkType\":1}," +
"{\"networkId\":\"4\",\"networkName\":\"another place\",\"networkType\":1}" +
"]" +
"}" +
"}";
public static void main(String[] args)
{
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
ContainerData container = gson.fromJson(jsonInput, ContainerData.class);
System.out.println(container);
}
}
class ContainerData
{
private boolean success;
private String reason;
private String testString;
private Data data;
@Override
public String toString()
{
return String.format(
"[ContainerData: success=%1$b, reason=%2$s, testString=%3$s, data=%4$s]",
success, reason, testString, data);
}
}
class Data
{
private Network[] networks;
@Override
public String toString()
{
return String.format(
"[Data: networks=%1$s]",
Arrays.toString(networks));
}
}
class Network
{
private String networkId;
private String networkName;
private int networkType;
@Override
public String toString()
{
return String.format(
"[Network: networkId=%1$s, networkName=%2$s, networkType=%3$d]",
networkId, networkName, networkType);
}
}
答案 1 :(得分:2)
我已经解析了上面提到的JSON字符串。它没有使用GSON来解析JSON。请试试这个。我认为它会影响你的问题:
String jsonString = "{\"testString\":\"some string\",\"success\":true,\"reason\":null,\"data\":{\"networks\":[{\"networkId\":\"1\",\"networkName\":\"some area\",\"networkType\":1},{\"networkId\":\"4\",\"networkName\":\"another place\",\"networkType\":1}]}}";
JSONObject jObject = new JSONObject(jsonString);
String menuObject = jObject.getString("testString");
System.out.println("testString="+menuObject);
String menuObject1 = jObject.getString("success");
System.out.println("success="+menuObject1);
String menuObject2 = jObject.getString("reason");
System.out.println("reason="+menuObject2);
String menuObject3 = jObject.getString("data");
System.out.println("data="+menuObject3);
JSONObject popupObject = jObject.getJSONObject("data");
JSONArray jObject1 = popupObject.getJSONArray("networks");
for (int i = 0; i < 2; i++) {
System.out.println("networkId of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkId").toString());
System.out.println("networkName of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkName").toString());
System.out.println("networkType of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkType").toString());
}