您的位置:首页 > Web前端 > JavaScript

JSON解析

2015-08-03 21:50 656 查看
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();
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: