Java json object operation

java json object operation

  • JSONObject class must reference the jar package
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-lang-2.5.jar
commons-logging.jar
ezmorph-1.0.3.jar
json-lib-2.1-jdk15.jar
  • java operation json need to introduce the following class library:
import net.sf.json.JSONArray;  
import net.sf.json.JSONException;  
import net.sf.json.JSONObject; 

josn common operation

package javaa;
import java.util.ArrayList;

import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; 
public class JsonObject {
     private static void testJsonBean() {
         /* *
          *
          * Create java object
          */Student student =new Student();  
            student.setId(1);  
            student.setName("李四");
            student.setSex("man");  
            student.setAge(25);  
            student.setHobby(new String[] { "篮球","网","跑","游戏"}); /**
             *java object is converted to json object and gets json object property
             */JSONObject jsonObject = JSONObject.fromObject(student);
            System.out.println(jsonObject.toString());//Get the properties of the jsonObject objectSystem.out.println(jsonObject.getString("id"));
            System.out.println(jsonObject.getJSONArray("hobby"));
            /**
             * json object is converted to java object and get object properties
             */Student student2 = (Student) JSONObject.toBean(jsonObject,Student.class);
            System.out.println(student2);

            Student student3 =new Student();
            student3.setAge(40);
            student3.setId(2);
            student3.setName("zane");
            student3.setSex("female");
            student3.setHobby(new String[]{"rnning","jogging"});

            ArrayList<Student> list = new ArrayList<Student>();
            list.add(student);
            list.add(student3);
            /**
             * Convert list to jsonarray
             */JSONArray jsonArray = JSONArray.fromObject(list);
            System.out.println(jsonArray.toString());/**
             * jsonArray is converted to list
             */List<Student> list2 = ((List) JSONArray.toCollection(jsonArray,Student.class));
            System.out.println(list2);
     }public static void main(String[] args) {
        /**
         * New JSONObject() object
         */JSONObject jsonObj =new JSONObject();  
         jsonObj.put("id", 1);  
         jsonObj.put("name", "张勇");
         jsonObj.put("sex", "男");
         jsonObj.put("age", 24); 
         jsonObj.put("hobby", new String[]{"网","游戏","跑","音乐"});
         System.out.println(jsonObj.toString());/**
           * Add elements to the JSONObject object
           */jsonObj.element("address", "xidian");
         System.out.println(jsonObj.toString());

         /**
          * Convert a JSONObject object to a Student object
          */Student student = (Student) JSONObject.toBean(jsonObj,Student.class);
         System.out.println(student);

         JsonObject.testJsonBean();

    }

}

JSONObject and JSONArray use

  1. JSONObject and JSONArray difference
    • JSONObject data is represented by { }, for example: { "id": "123", "courseID": " Huangt-test", "title": "submit job", "content": null }
    • JSONArray, as the name suggests is an array of JSONObject, represented by [ { } , { } , ...... , { } ] , for example: [ { "id" : "123", "courseID": "huangt-test", "title": "submit job" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ]
  2. How to get JSONObject object and JSONArray object from string String
JSONObject  jsonObject  = new JSONObject ( String  str);
JSONArray jsonArray = new JSONArray(String    str  ) ;
  1. How to get JSONObject from JSONArray You can treat JSONArray as a normal array, but the method of getting the data in the data is not the same
JSONObject   jsonObject  =  jsonArray.getJSONObject(i);  
  1. Get the data in json
int   mid= jsonObject.getInt ( "id" ) ;   
String  mcourse=jsonObject.getString( " courseID") ;