android怎样解析复杂JSON数据,层数比较多的 20
{"data":{"flux":[{"time":{"start":"2016-10-1317:02:33","end":"2016-10-1417:02:33"},"s...
{"data":{"flux":[{"time":{"start":"2016-10-13 17:02:33","end":"2016-10-14 17:02:33"},"sum":{"analytical_uantity":"10955","attack_query":0},"normal_data":[{"sum":0,"time":"10-13 17:02"},{"sum":0,"time":"10-13 20:28"},{"sum":0,"time":"10-13 23:53"},{"sum":0,"time":"10-14 03:19"},{"sum":0,"time":"10-14 06:45"},{"sum":0,"time":"10-14 10:11"},{"sum":"10955","time":"10-14 13:36"},{"sum":0,"time":"10-14 17:02"}],"attack_data":[{"sum":0,"time":"10-13 17:02"},{"sum":0,"time":"10-13 20:28"},{"sum":0,"time":"10-13 23:53"},{"sum":0,"time":"10-14 03:19"},{"sum":0,"time":"10-14 06:45"},{"sum":0,"time":"10-14 10:11"},{"sum":0,"time":"10-14 13:36"},{"sum":0,"time":"10-14 17:02"}]},{"time":{"start":"2016-09-14 17:02:34","end":"2016-10-14 17:02:34"},"sum":{"analytical_uantity":"10955","attack_query":0},"normal_data":[{"sum":0,"time":"09-14 17:02"},{"sum":0,"time":"09-18 23:53"},{"sum":0,"time":"09-23 06:45"},{"sum":0,"time":"09-27 13:36"},{"sum":0,"time":"10-01 20:28"},{"sum":0,"time":"10-06 03:19"},{"sum":0,"time":"10-10 10:11"},{"sum":"10955","time":"10-14 17:02"}],"attack_data":[{"sum":0,"time":"09-14 17:02"},{"sum":0,"time":"09-18 23:53"},{"sum":0,"time":"09-23 06:45"},{"sum":0,"time":"09-27 13:36"},{"sum":0,"time":"10-01 20:28"},{"sum":0,"time":"10-06 03:19"},{"sum":0,"time":"10-10 10:11"},{"sum":0,"time":"10-14 17:02"}]}]},"info":"success","status":"success"}
展开
2016-10-20 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
1使用android内置的org.json包提供的类解析,并保存在指定的对象中
Response response = new Response();
// 解析response_head
Response_head response_head = new Response_head();
JSONObject jsonObject = new JSONObject(json);
JSONObject responseHead = jsonObject.getJSONObject("response_head");
response_head.setRespmenu(responseHead.getString("respmenu"));
response_head.setResptime(responseHead.getString("resptime"));
Respinfo respInfo = new Respinfo();
JSONObject respinfo = responseHead.getJSONObject("respinfo");
respInfo.setRespcode(respinfo.getString("respcode"));
respInfo.setRespdes(respinfo.getString("respdes"));
response_head.setRespinfo(respInfo);
response.setResponse_head(response_head);
// 解析response_body
Response_body response_body = new Response_body();
JSONObject responseBody = jsonObject.getJSONObject("response_body");
JSONArray jsonArray = responseBody.getJSONArray("crset");
List<Merchant> merchants = new ArrayList<Merchant>();
for (int i = 0; i < jsonArray.length(); i++) {
// 解析每个merchant
JSONObject item = jsonArray.getJSONObject(i);
Merchant merchant = new Merchant();
merchant.setMerchantname(item.getString("merchantname"));
// 解析每个menu
List<Menu> menus = new ArrayList<Menu>();
JSONArray array = item.getJSONArray("menu");
for (int j = 0; j < array.length(); j++) {
JSONObject menuitem = array.getJSONObject(i);
Menu menu = new Menu();
menu.setMenuid(menuitem.getString("menuid"));
menu.setMenuname(menuitem.getString("menuname"));
menus.add(menu);
}
merchant.setMenu(menus);
merchants.add(merchant);
}
response_body.setCrset(merchants);
response.setResponse_body(response_body);12345678910111213141516171819202122232425262728293031323334353637381234567891011121314151617181920212223242526272829303132333435363738
2 使用google的开源库gson解析,并保存在指定的对象中
Gson gson = new Gson();
Type type = new TypeToken<Response>(){}.getType();
Response response = gson.fromJson(json, type);
Response response = new Response();
// 解析response_head
Response_head response_head = new Response_head();
JSONObject jsonObject = new JSONObject(json);
JSONObject responseHead = jsonObject.getJSONObject("response_head");
response_head.setRespmenu(responseHead.getString("respmenu"));
response_head.setResptime(responseHead.getString("resptime"));
Respinfo respInfo = new Respinfo();
JSONObject respinfo = responseHead.getJSONObject("respinfo");
respInfo.setRespcode(respinfo.getString("respcode"));
respInfo.setRespdes(respinfo.getString("respdes"));
response_head.setRespinfo(respInfo);
response.setResponse_head(response_head);
// 解析response_body
Response_body response_body = new Response_body();
JSONObject responseBody = jsonObject.getJSONObject("response_body");
JSONArray jsonArray = responseBody.getJSONArray("crset");
List<Merchant> merchants = new ArrayList<Merchant>();
for (int i = 0; i < jsonArray.length(); i++) {
// 解析每个merchant
JSONObject item = jsonArray.getJSONObject(i);
Merchant merchant = new Merchant();
merchant.setMerchantname(item.getString("merchantname"));
// 解析每个menu
List<Menu> menus = new ArrayList<Menu>();
JSONArray array = item.getJSONArray("menu");
for (int j = 0; j < array.length(); j++) {
JSONObject menuitem = array.getJSONObject(i);
Menu menu = new Menu();
menu.setMenuid(menuitem.getString("menuid"));
menu.setMenuname(menuitem.getString("menuname"));
menus.add(menu);
}
merchant.setMenu(menus);
merchants.add(merchant);
}
response_body.setCrset(merchants);
response.setResponse_body(response_body);12345678910111213141516171819202122232425262728293031323334353637381234567891011121314151617181920212223242526272829303132333435363738
2 使用google的开源库gson解析,并保存在指定的对象中
Gson gson = new Gson();
Type type = new TypeToken<Response>(){}.getType();
Response response = gson.fromJson(json, type);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询