首页 > Web开发 > 详细

json 及json数据解析

时间:2019-01-29 22:49:51      阅读:192      评论:0      收藏:0      [点我收藏+]

1、json数字

{ "age":30 }

2、json对象

{ "name":"快递6666" , "url":"www.kuidi6666.com" }

3、json数组

{ "sites": [ { "name":"学习java" , "url":"www.xuexi.com" }, { "name":"google" , "url":"www.xiaoyou.com" }, { "name":"微博" , "url":"www.weibo.com" } ] }

 

4、json解析

String s = "{\"error\":0,\"status\":\"success\",\"results\":[{\"currentCity\":\"青岛\",\"index\":[{\"title\":\"穿衣\",\"zs\":\"较冷\",\"tipt\":\"穿衣指数\",\"des\":\"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\"},{\"title\":\"紫外线强度\",\"zs\":\"最弱\",\"tipt\":\"紫外线强度指数\",\"des\":\"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。。\"}],}]}";

JSONObject jsonObject = JSONObject.fromObject(s); //提取出error为 0

int error = jsonObject.getInt("error");

System.out.println("error:" + error);

//提取出status为 success

String status = jsonObject.getString("status");

System.out.println("status:" + status);

//注意:results中的内容带有中括号[],所以要转化为JSONArray类型的对象

JSONArray result = jsonObject.getJSONArray("results");

for (int i = 0; i < result.size(); i++) {

//提取出currentCity为 青岛

String currentCity = result.getJSONObject(i).getString("currentCity");

System.out.println("currentCity:" + currentCity);

//注意:index中的内容带有中括号[],所以要转化为JSONArray类型的对象

JSONArray index = result.getJSONObject(i).getJSONArray("index");

for (int j = 0; j < index.size(); j++) {

String title = index.getJSONObject(j).getString("title");

System.out.println("title:" + title);

}

}

通过以上步骤即可完成json多层嵌套的解析!!

 

json 及json数据解析

原文:https://www.cnblogs.com/qqzhulu/p/10336162.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!