package com.lingzhuo.jsontest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONTest {
public static void main(String[] args) {
String json = CreatJSON();
JSONObject obj = JSONObject.fromObject(json);
System.out.println(obj.getString("city"));
// System.out.println(obj.getString("today"));
JSONObject today = obj.getJSONObject("today");
JSONArray array = today.getJSONArray("index");
for (int i = 0; i < array.size(); i++) {
JSONObject object = array.getJSONObject(i);
System.out.println(object.getString("name"));
}
}
private static String CreatJSON() {
JSONObject obj=new JSONObject();
obj.put("city","北京");
obj.put("cityid", "123");
JSONObject today=new JSONObject();
today.put("date", "2015-08-03");
today.put("week", "星期一");
JSONArray array=new JSONArray();
JSONObject index1=new JSONObject();
index1.put("name", "感冒");
JSONObject index2=new JSONObject();
index2.put("name", "防晒");
JSONObject index3=new JSONObject();
index3.put("name", "炎热");
array.add(index1);
array.add(index2);
array.add(index3);
today.put("index", array);
obj.put("today", today);
// System.out.println(obj.toString());
return obj.toString();
}
}
JSON解析
最新推荐文章于 2024-09-13 17:32:25 发布