JSON解析

来源:互联网 发布:sql语句大全实例教程 编辑:程序博客网 时间:2024/06/08 03:15
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();    }}
0 0
原创粉丝点击