我有一个Json对象,其中包含以下数据
{\“ data \”:{\“ pdp0:\”:{\“ recv \”:{\“ packets \”:\“ 15104 \”,\“ bytes \”:\“ 9059817 \”},\“ send \“:{\” 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}
如果要访问data-> pdp0-> recv-> packets(此处的值为15104)中的数据,应该使用哪个函数?
谢谢
萨拉斯
最佳答案
您可以使用普通的旧org.json包来获取嵌套的对象:
int packets = new JSONObject(str) // str is your JSON string as above
.getJSONObject("data")
.getJSONObject("pdp0")
.getJSONObject("recv")
.getInt("packets");
此处的API详细信息:http://www.json.org/javadoc/org/json/JSONObject.html