JSONObject和JSONArray的使用 以及JSONObject put,accumulate,element的区别
2016-05-22 13:20
525 查看
JSONObject是java用来处理json对象的,在java中如果用到json需要引入json包。可以到这里下载
下面代码是JSONObject、JSONArray的简单使用,以及以及JSONObject put,accumulate,element的区别
public static void main(String[] arg){
JSONObject jsonAddr = new JSONObject();
jsonAddr.put("city", "厦门市");
jsonAddr.put("street", "博物馆");
JSONObject json = new JSONObject();
json.put("user", "wzj");//增加key值
json.put("age", 12);
json.put("addr", jsonAddr); //增加一个对象到该key下,嵌套json
json.put("tel", "1865010");
json.accumulate("tel", "1516003");//积累这个value到该key下,如果key存在,则该key的值为数组
json.put("age", 20);//如果该key已经存在则覆盖原来的值
json.put("lover", "secret");
json.element("lover", "secret2");//这个看文档是说,如果值原来存在的话会调用accumulate,但发现这里其实没有调用accumulate?? 不知道是怎么回事?
JSONArray jsonArray = new JSONArray();
jsonArray.add(0,"person1");
jsonArray.add(1,"person2");
json.put("friend", jsonArray);//增加一个数组
System.out.println("json="+json);
System.out.println("json.getString(\"user\")="+json.getString("user"));
System.out.println("json.getJSONArray(\"friend\").get(0)="+json.getJSONArray("friend").get(0));
System.out.println("json.getJSONObject(\"addr\").get(\"city\")="+json.getJSONObject("addr").get("city"));
}
输出:
json={"user":"wzj","age":20,"addr":{"city":"厦门市","street":"博物馆"},"tel":["1865010","1516003"],"lover":"secret2","friend":["person1","person2"]}
json.getString("user")=wzj
json.getJSONArray("friend").get(0)=person1
json.getJSONObject("addr").get("city")=厦门市
下面代码是JSONObject、JSONArray的简单使用,以及以及JSONObject put,accumulate,element的区别
public static void main(String[] arg){
JSONObject jsonAddr = new JSONObject();
jsonAddr.put("city", "厦门市");
jsonAddr.put("street", "博物馆");
JSONObject json = new JSONObject();
json.put("user", "wzj");//增加key值
json.put("age", 12);
json.put("addr", jsonAddr); //增加一个对象到该key下,嵌套json
json.put("tel", "1865010");
json.accumulate("tel", "1516003");//积累这个value到该key下,如果key存在,则该key的值为数组
json.put("age", 20);//如果该key已经存在则覆盖原来的值
json.put("lover", "secret");
json.element("lover", "secret2");//这个看文档是说,如果值原来存在的话会调用accumulate,但发现这里其实没有调用accumulate?? 不知道是怎么回事?
JSONArray jsonArray = new JSONArray();
jsonArray.add(0,"person1");
jsonArray.add(1,"person2");
json.put("friend", jsonArray);//增加一个数组
System.out.println("json="+json);
System.out.println("json.getString(\"user\")="+json.getString("user"));
System.out.println("json.getJSONArray(\"friend\").get(0)="+json.getJSONArray("friend").get(0));
System.out.println("json.getJSONObject(\"addr\").get(\"city\")="+json.getJSONObject("addr").get("city"));
}
输出:
json={"user":"wzj","age":20,"addr":{"city":"厦门市","street":"博物馆"},"tel":["1865010","1516003"],"lover":"secret2","friend":["person1","person2"]}
json.getString("user")=wzj
json.getJSONArray("friend").get(0)=person1
json.getJSONObject("addr").get("city")=厦门市
相关文章推荐
- Objective-C ---代理和协议 (梳理整理)
- Objective-C ---点语法 (梳理整理)
- QObject源码分析
- Programming Scala第4章demo03 单例对象object
- javascript之object
- Objective-C ---点语法 (梳理整理)
- Objective-C ---代理和协议 (梳理整理)
- ObjectC Hook函数的实现与实战
- NSObject的方法说明
- 讲的很详细的一篇关于object equals() & hashCode() 的文章
- 使用cx_freeze工具打包时提示 AttributeError 'module' object has no attribute '_fix_up_module':
- Object Following for UAV(无人机物体跟随)
- iOS多线程之NSThread、NSOperationQueue、NSObject和GCD的区别
- Volley(2)—JsonObjectRequest和ImageRequest
- spring的 object is not an instance of declaring class
- 不管在不在arc下,object对象都有强引用、弱引用之分
- Xcode:duplicate declaration of method 'XXX'
- 重写Object类中的equals方法
- Programming with Objective-C(七)
- Objective-C Coding Style Guidelines