Reputation:
I have a Json object which has the following data
{\"data\":{\"pdp0:\":{\"recv\":{\"packets\":\"15104\",\"bytes\":\"9059817\"},\"sent\":{\"packets\":\"9480\",\"bytes\":\"1687801\"}},\"gre0:\":{\"recv\":{\"packets\":\"0\",\"bytes\":\"0\"},\"sent\":{\"packets\":\"0\",\"bytes\":\"0\"}},\"tunl0:\":{\"recv\":{\"packets\":\"0\",\"bytes\":\"0\"},\"sent\":{\"packets\":\"0\",\"bytes\":\"0\"}},\"usb0:\":{\"recv\":{\"packets\":\"0\",\"bytes\":\"0\"},\"sent\":{\"packets\":\"0\",\"bytes\":\"0\"}},\"lo:\":{\"recv\":{\"packets\":\"48300\",\"bytes\":\"2616703\"},\"sent\":{\"packets\":\"48300\",\"bytes\":\"2616703\"}}},\"ver\":\"1.4\",\"type\":\"netdev\",\"date\":\"2011-4-13 14:10:21\",\"user\":\"351863047772880\",\"time_stamp\":1305313821541}
If I want to access the data in data->pdp0->recv->packets (here the value is 15104) which function should I use?
thanks sarath
Upvotes: 0
Views: 1289
Reputation: 3610
You can use what Aleadam suggested. However, you would be better of using GSON for java objects because it allows you to serialise and de-serialise your JSON to and from your Java Objects.
Upvotes: 1
Reputation: 40381
You can use the plain old org.json package to get the nested objects:
int packets = new JSONObject(str) // str is your JSON string as above
.getJSONObject("data")
.getJSONObject("pdp0")
.getJSONObject("recv")
.getInt("packets");
API details here: http://www.json.org/javadoc/org/json/JSONObject.html
Upvotes: 1