import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSON {
public static void main(String[] args) {
//解析JSON
String json=createJSON();
JSONObject obj=JSONObject.fromObject(json);
System.out.println(obj.get("city"));
JSONObject today=obj.getJSONObject("today");
JSONArray array=today.getJSONArray("index");
for (int i = 0; i < array.size(); i++) {
JSONObject obj1=array.getJSONObject(i);
System.out.println(obj1.get("name"));
}
}
//创建一个JSON
private static String createJSON(){
JSONObject obj=new JSONObject();
obj.put("city","北京");
obj.put("cityid","123");
JSONObject today=new JSONObject();
today.put("date","2015-08-09");
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();
}
}