解析JSON数据 [{}], {[]}格式
例子:
{“TaskId”:“75aa50d61921680000016aed10589025011”,“Unit”:“Kbps”,“Interval”:60,“Data”:[{“Ip”:“103.36.192.170”,“Data”:[[“2019-05-22T03:09:00Z”,0]]}]}
{"TaskId":"75aa50d61921680000016aed10589025011","Unit":"Kbps","Interval":60,"Data":[{"Ip":"103.36.192.170","Data":[["2019-05-22T03:09:00Z",0]]}]}
{
"TaskId":"75aa50d61921680000016aed10589025011",
"Unit":"Kbps",
"Interval":60,
"Data":[
{
"Ip":"103.36.192.170",
"Data":[
[
"2019-05-22T03:09:00Z",
0
]
]
}
]
}
JSON json = {"TaskId":"75aa50d61921680000016aed10589025011","Unit":"Kbps","Interval":60,"Data":[{"Ip":"103.36.192.170","Data":[["2019-05-22T03:09:00Z",0]]}]};
JSONObject jsonObject = JSON JSONObject.fromObject(json);
//jsonObject ------>{"TaskId":"75aa50d61921680000016aed10589025011","Data":[{"Ip":"103.36.192.170","Data":[["2019-05-22T03:09:00Z",0]]}],"Unit":"Kbps","Interval":60}
//获取Data的数据 由于Data是一个数组,因此用JSONArray来接收
JSONArray jsonArray = jsonObject.getJSONArray("Data");
//jsonArray------>[{"Ip":"103.36.192.170","Data":[["2019-05-22T03:09:00Z",0]]}]
//再拿Data里面的数据 因为jsonArray中只有一个对象 所有get(0)
Object obj = jsonArrat.get(0);
JSONObject jsonObject1 = JSON JSONObject.fromObject(obj);
JSONArray jsonArray1 = jsonObject.getJSONArray("Data");
//jsonArray1------>[["2019-05-22T03:09:00Z",0]]
这数据包含了大部分JSON格式