天天看點

JSONObject和JSONArray差別

一、使用JSONObject和JSONArray必須的jar包:

commons-beanutils.jar

commons-collections.jar

commons-lang.jar

commons-logging.jar  

ezmorph.jar

json-lib.jar

二、将字元串轉換成JSONObject,将JSONObject轉換成JSONArray

package com.company;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONTest {

    /**
    * @Description:
     * 取出name4值過程步驟:
     * 1,将以上字元串轉換為JSONArray對象;
     * 2,取出對象的第一項,JSONObject對象;
     * 3,取出name1的值JSONObject對象;
     * 4,取出name2的值JSONObject對象;
     * 5,取出name4的值value2。
    * @Param:
    * @return:
    * @Author: yaoXu
    * @Date: 2020/7/6
    */
    public static void main(String[] args) {

        String arrayStr = "[{name1:{name2:{name3:'value1',name4:'value2'}}},{}]";

        //将結果轉換成JSONArray對象的形式
        JSONArray getJsonArray = JSONArray.fromObject(arrayStr);

        //擷取json數組中的第一項
        JSONObject getJsonObj = getJsonArray.getJSONObject(0);

        //此時,result已經是一個json對象
        JSONObject result = getJsonObj.getJSONObject("name1").getJSONObject("name2");
        System.out.println(result);

        //擷取name4的值
        Object name4 = result.get("name4");
        System.out.println(name4);
    }
}
           

三、基礎用法

package com.company;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @program: test
 * @description
 * @author: yaoXu
 * @create: 2020-07-06 10:39
 **/
public class JSONTest_2 {

    //建立JSONObject對象
    private static JSONObject createJSONObject(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username","erHa");
        jsonObject.put("sex", "男");
        jsonObject.put("QQ", "6666666");
        jsonObject.put("Min.score", new Integer(99));
        jsonObject.put("nickname", "erHaChiRouRou");
        return jsonObject;
    }

    public static void main(String[] args) {

        JSONObject jsonObject = JSONTest_2.createJSONObject();
        //輸出jsonObject對象
        System.out.println("Json對象(JSONObject)==>"+jsonObject);

        //判讀輸出對象的類型
        boolean isArray = jsonObject.isArray();
        boolean isEmpty = jsonObject.isEmpty();
        boolean isNullObject = jsonObject.isNullObject();
        System.out.println("isArray:"+isArray +"\nisEmpty:"+isEmpty+
                "\nisNullObject:"+isNullObject);

        //添加屬性
        jsonObject.element("address", "湖南省長沙市");
        System.out.println("添加屬性後的對象==>"+jsonObject);

        //傳回一個JSONArray對象
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(0, "this is NO_0 jsonArray value");
        jsonArray.add(1,"this is NO_1 jsonArray value");
        jsonObject.element("jsonArray", jsonArray);
        JSONArray array = jsonObject.getJSONArray("jsonArray");
        System.out.println("傳回一個JSONArray對象:"+array);
        //添加JSONArray後的值
        //{"username":"huangwuyi","sex":"男","QQ":"999999999","Min.score":99,
        // "nickname":"夢中心境","address":"福建省廈門市","jsonArray":["this is a jsonArray value","another jsonArray value"]}
        System.out.println("結果="+jsonObject);

        //根據key傳回一個字元串
        String username = jsonObject.getString("username");
        System.out.println("username==>"+username);

        //把字元轉換為 JSONObject
        String temp=jsonObject.toString();
        JSONObject object = JSONObject.fromObject(temp);
        //轉換後根據Key傳回值
        System.out.println("qq="+object.get("QQ"));


    }

}