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(); } }
相关文章推荐
- js 重载问题
- JavaScript 堆内存分析新工具 OneHeap
- 对应答报文中实体信息的处理(json数据解析)
- JavaScript 堆内存分析新工具 OneHeap
- 浅谈JS闭包中的循环绑定处理程序
- servlet 传值给 jsp
- JavaScript中getBoundingClientRect()方法详解
- javascript里Dom问题之document.getElementById('xx').innerHTML
- js实现抢购倒计时精确到毫秒 到期提示“抢购已结束”
- 运用JS设置cookie、读取cookie、删除cookie
- javascript 没有块级作用域 JavaScript声明全局变量三种方式 及
- 如何使用Createjs来编写HTML5游戏(一)EaselJS简介
- 使用自带的JavaScriptSerializer序列化实体 指定的属性如何不序列化
- JavaScript向php传递参数
- JavaScript经典实例
- QJSON的编译方法【记录】
- 表格js插件highcharts
- javascript中命名陷阱
- HDU 4352 XHXJ's LIS 数位状压DP
- 《JS设计模式笔记》 3,观察者模式