I have a data structure as follows
{"testString":"some string开发者_StackOverflow中文版","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?
Here's an approach that works with the JSON input you provided, but more "fully" deserializes into a friendly data structure.
The output is
[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);
}
}
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:
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());
}
精彩评论