JSON解析工具-org.json 解读

来源:互联网 发布:php 查找中文字符串 编辑:程序博客网 时间:2024/05/16 18:29

一、简介 

org.json是Java常用的Json解析工具,主要提供JSONObject和JSONArray类,现在就各个类的使用解释如下 

二、准备 

1.在使用org.json之前,我们应该先从该网址https://github.com/douglascrockford/JSON-java下载org.json源码,并将源码其加入到Eclipse中,即可调用 

2.查看相关的API文档,访问:https://github.com/douglascrockford/JSON-java。 

三、讲解 

1.JSONObject: 

  • 是一个无序的键/值对集合 

  • 它的表现形式是一个包裹在花括号的字符串,键和值之间使用冒号隔开,键值和键值之间使用逗号隔开 

  • 内在形式是一个使用get()和opt()方法通过键来访问值,和使用put()方法通过键来添加或者替代值的对象 

  • 值可以是任何这些类型:Boolean,JSONArray,JSONObject,Number和String,或者JOSONObject.NULL对象。 

代码演示如下 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void jsonObjectTest() {   
  2. ᅠ ᅠ JSONObject jsonobj = new JSONObject("{'name':'xiazdong','age':20}");   
  3.     String name = jsonobj.getString("name");   
  4. ᅠ ᅠ int age = jsonobj.getInt("age");   
  5.     System.out.println("name = " + name + ",age = " + age);   
  6. }   

注:JSONObject有很多optXXX方法,比如optBooleanoptStringoptInt... 

他们的意思是,如果这个jsonObject有这个属性,则返回这个属性,否则返回一个默认值 

2.JSONArray: 

  • 是一个有序的序列值 

  • 它的表现形式是一个包裹在方括号的字符串,值和值之间使用逗号隔开 

  • 内在形式是一个使用get()和opt()方法通过索引来访问值,和使用put()方法来添加或修改值的对象 

  • 值可以是任何这些类型:Boolean,JSONArray,JSONObject,Number,和String,或者JSONObject.NULL对象。 

代码演示如下 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void jsonArrayTest() {   
  2. ᅠ ᅠ JSONArray jsonarray = new JSONArray("[{'name':'xiazdong','age':20},{'name':'xzdong','age':15}]");   
  3. ᅠ ᅠ for (int i = 0; i < jsonarray.length(); i++) {   
  4. ᅠ ᅠ ᅠ ᅠᅠJSONObject jsonobj = jsonarray.getJSONObject(i);   
  5.         String name = jsonobj.getString("name");   
  6.         int age = jsonobj.getInt("age");   
  7.         System.out.println("name = " + name + ",age = " + age);   
  8.     }   
  9. }   

嵌套的JSONObject和JSONArray代码演示如下 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void jsonObjectAndArrayTest() {   
  2. ᅠ ᅠ String jsonstring = "{'name':'xiazdong','age':20,'book':['book1','book2']}";   
  3. ᅠ ᅠ JSONObject jsonobj = new JSONObject(jsonstring);   
  4.    
  5.     String name = jsonobj.getString("name");   
  6. ᅠ ᅠ System.out.println("name" + ":" + name);   
  7.    
  8.     int age = jsonobj.getInt("age");   
  9.     System.out.println("age" + ":" + age);   
  10.    
  11. ᅠ ᅠ JSONArray jsonarray = jsonobj.getJSONArray("book");   
  12. ᅠ ᅠ for (int i = 0; i < jsonarray.length(); i++) {   
  13.         String book = jsonarray.getString(i);   
  14. ᅠ ᅠ ᅠ ᅠ System.out.println("book" + i + ":" + book);   
  15.     }    
  16. } <span style="font-size: 11pt; line-height: 17px; background-color: transparent; color: windowtext; font-family: Calibri, sans-serif;"> </span>  

3.JSONStringer: 

  • 是一个用于快速构造JSON文本的工具 

  • JSONWriter的子类 

  • bject():开始一个对象,即添加{;enObject():结束一个对象,即添加} 

  •  array():开始一个数组,即添加[; endArray():结束一个数组,即添加] 

  • key():表示添加一个key;value():表示添加一个value 

代码演示如下 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void jsonStringerTest() {   
  2. ᅠ ᅠ JSONStringer stringer = new JSONStringer();   
  3.     stringer.object().key("name").value("xiazdong").key("age").value(20).endObject();   
  4. ᅠ ᅠ System.out.println(stringer);   
  5. } <span style="font-size: 11pt; line-height: 17px; background-color: transparent; color: windowtext; font-family: Calibri, sans-serif;"> </span>  

负载的JSON格式写演示(PrintWriter+JSONStringer可以写入JSON文件): 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void jsonStringerTest2() throws FileNotFoundException {   
  2. ᅠ ᅠ JSONStringer jsonStringer = new JSONStringer();   
  3. ᅠ ᅠ JSONObject obj6 = new JSONObject();   
  4.     obj6.put("title", "book1").put("price", "$11");   
  5. ᅠ ᅠ JSONObject obj3 = new JSONObject();   
  6.     obj3.put("book", obj6);   
  7. ᅠ ᅠ obj3.put("author", new JSONObject().put("name", "author-1"));   
  8.    
  9. ᅠ ᅠ JSONObject obj5 = new JSONObject();   
  10.     obj5.put("title", "book2").put("price", "$22");   
  11. ᅠ ᅠ JSONObject obj4 = new JSONObject();   
  12.     obj4.put("book", obj5);   
  13.  ᅠ ᅠobj4.put("author", new JSONObject().put("name", "author-2"));   
  14.    
  15. ᅠ ᅠ JSONArray obj2 = new JSONArray();   
  16.     obj2.put(obj3).put(obj4);   
  17.    
  18. ᅠ ᅠ JSONObject obj1 = new JSONObject();   
  19.     obj1.put("title", "BOOK");   
  20.     obj1.put("signing", obj2);   
  21.    
  22.     jsonStringer.object().key("session").value(obj1).endObject();   
  23.     System.out.println(jsonStringer.toString());   
  24.    
  25. ᅠ ᅠ PrintWriter out = new PrintWriter(new FileOutputStream("1.txt"));   
  26. ᅠ ᅠ out.println(jsonStringer.toString());   
  27.     out.close();   
  28. } <span style="font-size: 11pt; line-height: 17px; background-color: transparent; color: windowtext; font-family: Calibri, sans-serif;"> </span>  

4.JSONTokener 

  • 它和JSONObject和JSONArray的构造函数一起使用,用于解析JSON源字符串 

代码演示如下(JSONObject+JSONTokener能够获取JSON格式文本对象): 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void JSONTokenerTest() throws FileNotFoundException {   
  2.     JSONObject jsonobj = new JSONObject(new JSONTokener(new FileReader(new File("1.txt"))));   
  3.     System.out.println(jsonobj.getJSONObject("session").getJSONArray("signing").getJSONObject(1).getJSONObject("book").getString("title"));   
  4. }   

注意:在Java中,JSON格式的字符串最好用单引号表示 

0 0
原创粉丝点击