Java转json

一、json介绍

       1、 作用:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,是存储和交换文本信息的语法。    

   2、json以key-value的格式书写,数据间以“,”分开,有两种数据结构:

   对象:“{}”括起来的内容

   数组:“[]”括起来的内容

     如:{

           "people":[
              {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},
              {"firstName":"Jason","lastName":"Hunter","email":"bbbb"},
              {"firstName":"Elliotte","lastName":"Harold","email":"cccc"}
           ]
       }
 

二、org.json.simple.JSON....与net.sf.json.JSON...的区别

       1、json的创建

            org.json.simple.JSONObject json = new JSONObject();           json.put(key,value);

            net.sf.json.JSONObject json = JSONObject.fromObject(str);  

            net.sf.json.jsonobject 没有 new JSONObject(String)的构造方法

       2、解析基本差不多

       注:在封装数据是net.sf.json.JSON...明显高效与org.json.simple.JSON....

三、json需要的包

       1、org.json.simple.JSONObject和org.json.simple.JSONArray

    

   2、net.sf.json.JSONObject和net.sf.json.JSONArray

             

             缺commons-lang-2.4.jar包:Exception in thread "main" java.lang.NoClassDefFoundError:             org/apache/commons/lang/exception/NestableRuntimeException

      缺ezmorph-1.0.6.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher

      缺commons-logging-1.1.1.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

      缺commons-collections-3.1.jar包:Exception in thread "main" java.lang.NoClassDefFoundError:             org/apache/commons/collections/map/ListOrderedMap

      缺commons-beanutils-1.8.0.jar包:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean

 

       参考自: JSON_百度百科    JSON中国 | JSON中文网     JSON 教程

四、实例介绍

1、JSONtest类

public class JSONtest {
     public static void main(String[] args) {
         //将Java对象转换成JSONObject对象并输出
         javaObjectToJSONObject(); 

                               
        //将javaObjectList转换为JSONArray并输出
        javaObjectListToJSONArray();
  
        //将Java对象转换成JSONObject对象并输出
        javaObjectToJsonObject();
  
        //将javaObjectList转换为JSONArray并输出
        javaObjectListToJsonArray();
  
        //stringArray转换为net.sf.json.JSONArray并输出
        stringArrayToJsonArray();
  
        //将stringArray转换为org.json.simple.JSONArray
        stringArrayToJSONArray();
    }
 
 
 public static void stringArrayToJSONArray(){
      String[] strArr = {"s1","s2","s3"};
      org.json.simple.JSONArray jsonArray = new org.json.simple.JSONArray();
      for(int i = 0; i < strArr.length; i++){
           jsonArray.add(strArr[i]);
      }
      System.out.println("jsonArray = "+jsonArray);
      int len = jsonArray.size();
      for(int i = 0; i < len; i++){
          System.out.println(jsonArray.get(i));
      }
 }

输出为:

jsonArray = ["s1","s2","s3"]
s1
s2
s3
 
 
 public static void stringArrayToJsonArray(){
      String[] strArr = {"s1","s2","s3"};
      net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(strArr);
      System.out.println("jsonArray = "+jsonArray);
      int len = jsonArray.size();
      for(int i = 0; i < len; i++){
           System.out.println(jsonArray.get(i));
      }
 }
 输出为:

jsonArray = ["s1","s2","s3"]
s1
s2
s3


 
 public static void javaObjectListToJsonArray(){
      List personList = createPersonList();
      net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(personList);
      System.out.println("net.sf.json.JSONArray \n jsonArray = "+jsonArray);
      int len = jsonArray.size();
      for(int i = 0; i < len; i++){
          net.sf.json.JSONObject jsonObject = (net.sf.json.JSONObject)jsonArray.get(i);
          System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "

                                +jsonObject.get("age"));
      }
 }

输出为net.sf.json.JSONArray :
 jsonArray = [{"age":0,"id":0,"userName":"shaoyesun0"},{"age":1,"id":1,"userName":"shaoyesun1"},{"age":2,"id":2,"userName":"shaoyesun2"},{"age":3,"id":3,"userName":"shaoyesun3"},{"age":4,"id":4,"userName":"shaoyesun4"},{"age":5,"id":5,"userName":"shaoyesun5"},{"age":6,"id":6,"userName":"shaoyesun6"},{"age":7,"id":7,"userName":"shaoyesun7"},{"age":8,"id":8,"userName":"shaoyesun8"},{"age":9,"id":9,"userName":"shaoyesun9"}]
0 | shaoyesun0 | 0
1 | shaoyesun1 | 1
2 | shaoyesun2 | 2
3 | shaoyesun3 | 3
4 | shaoyesun4 | 4
5 | shaoyesun5 | 5
6 | shaoyesun6 | 6
7 | shaoyesun7 | 7
8 | shaoyesun8 | 8
9 | shaoyesun9 | 9
 
 
 public static void javaObjectToJsonObject(){
      Person person = createPerson();
      net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(person);
      System.out.println("net.sf.json.JSONObject \n jsonObject = "+jsonObject);
      System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "

                         +jsonObject.get("age"));
 }

输出为net.sf.json.JSONObject: 
 jsonObject = {"age":1,"id":1,"userName":"shaoyesun1"}
1 | shaoyesun1 | 1
 
 
 public static void javaObjectListToJSONArray(){
       List personList = createPersonList();
       org.json.simple.JSONArray jsonArray = new org.json.simple.JSONArray();
       for(Person person : personList){
           org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
           jsonObject.put("id", person.getId());
           jsonObject.put("userName", person.getUserName());
           jsonObject.put("age", person.getAge());
           jsonArray.add(jsonObject);
      }
      System.out.println("org.json.simple.JSONArray \n jsonArray = "+jsonArray);
      int len = jsonArray.size();
      for(int i = 0; i < len; i++){
          org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject)jsonArray.get(i);
          System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "

                            +jsonObject.get("age"));
  }
 }

输出为org.json.simple.JSONArray: 
 jsonArray = [{"id":0,"userName":"shaoyesun0","age":0},{"id":1,"userName":"shaoyesun1","age":1},{"id":2,"userName":"shaoyesun2","age":2},{"id":3,"userName":"shaoyesun3","age":3},{"id":4,"userName":"shaoyesun4","age":4},{"id":5,"userName":"shaoyesun5","age":5},{"id":6,"userName":"shaoyesun6","age":6},{"id":7,"userName":"shaoyesun7","age":7},{"id":8,"userName":"shaoyesun8","age":8},{"id":9,"userName":"shaoyesun9","age":9}]
0 | shaoyesun0 | 0
1 | shaoyesun1 | 1
2 | shaoyesun2 | 2
3 | shaoyesun3 | 3
4 | shaoyesun4 | 4
5 | shaoyesun5 | 5
6 | shaoyesun6 | 6
7 | shaoyesun7 | 7
8 | shaoyesun8 | 8
9 | shaoyesun9 | 9

  
 public static void javaObjectToJSONObject(){
      Person p = createPerson();
      org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
      jsonObject.put("id", p.getId());
      jsonObject.put("userName", p.getUserName());
      jsonObject.put("age", p.getAge());
      System.out.println("org.json.simple.JSONObject \n jsonObject = "+jsonObject);
      System.out.println(jsonObject.get("id")+" | "+jsonObject.get("userName")+" | "

                        +jsonObject.get("age"));
 }

                                                                                输出为org.json.simple.JSONObject :
                                                                                jsonObject = {"id":1,"userName":"shaoyesun1","age":1}
                                                                                1 | shaoyesun1 | 1
 
 
 public static Person createPerson(){
      Person person = new Person();
      person.setId(1);
      person.setUserName("shaoyesun1");
      person.setAge(1);
      return person;
 }
 
 
 public static List createPersonList(){
      List personList = new ArrayList();
      for(int i = 0; i < 10; i++){
          Person person = new Person();
          person.setId(i);
          person.setUserName("shaoyesun"+i);
          person.setAge(i);
          personList.add(person);
      }
      return personList;
 }
 
}

2、Person类

public class Person {
 
     private long id;
     private String userName;
     private int age;

 

   

}

posted @ 2016-04-17 21:52  code.world  Views(671)  Comments(0Edit  收藏  举报