JSON 的使用方法


JSON--JavaScript Object Notation,是一種輕量級的數據交互格式,本質是特定格式的字符串,相比xml更簡潔,現在是客戶端與服務器端交互的最常用選擇,已經很少用xml了

JSON格式:1.JSON對象{key1:value1,key2:value2,}   2.JSON數組[value1,value2]

先考入jar包,其中json.jar是官方jar包,fastjson.jar是阿里巴巴開發的jar包,在解析JSON數據時,官方包不如阿里巴巴的使用方便,可以通過如下代碼進行比較:

 

 

package com.hanqi.test;

public class User {

    private int userID;
    private String userName,password;
    public User(int userID, String userName, String password) {
        super();
        this.userID = userID;
        this.userName = userName;
        this.password = password;
    }
    public int getUserID() {
        return userID;
    }
    public void setUserID(int userID) {
        this.userID = userID;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public User() {
        super();
    }
    @Override
    public String toString() {
        return "User [userID=" + userID + ", userName=" + userName + ", password=" + password + "]";
    }
}
package com.hanqi.test;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class TestJSON {

    public static void main(String[] args) {
        //測試JSON解析 
        
        //1從對象(集合)到JSON字符串
        
        User u1 = new User(999,"tom","123456");
        
        //導入阿里巴巴   JSON對象  jar包
        String ju1=JSONObject.toJSONString(u1);
        System.out.println("ju1="+ju1);
        
        //集合
        List<User> lu = new ArrayList<User>();
        lu.add(new User(111,"User1","111"));
        lu.add(new User(222,"User2","111"));
        lu.add(new User(333,"User3","111"));
        lu.add(new User(444,"User4","111"));

        //導入阿里巴巴 JSON集合  jar包
        String jlu = JSONArray.toJSONString(lu);
        System.out.println("jlu="+jlu);
        
        //2從JSON字符串到集合(對象)
        //阿里巴巴JSON 可以直接將JSON字符串轉為對象
        User u2 =JSONObject.parseObject(ju1,User.class);
        System.out.println("u2="+u2);
        
        
        try {
            //名字沖突使用全路徑  官方jar包
            //官方jar包不能直接轉為對象,只能獲取對象的單個值
            org.json.JSONObject jo = new org.json.JSONObject(ju1);
            
            int userid = jo.getInt("userID");            //只能獲取單個值
            System.out.println("userID="+userid);
            
            
        } catch (JSONException e) {
            // TODO 自動生成的 catch 塊
            e.printStackTrace();
        }
        //字符串集合
        //使用阿里巴巴jar包可以直接得到對象的集合
        List<User> lu2 = JSONArray.parseArray(jlu,User.class);
        //遍歷集合
        for(User u:lu2)
        {
            
            System.out.println(u);
        }
        
        try {
            //使用官方jar包 必須解析JSON數組
            org.json.JSONArray ja = new org.json.JSONArray(jlu);
            
            //使用官方jar包   解析JSON數組時一次只能獲取其中的一個JSON對象
            org.json.JSONObject  u3=  ja.getJSONObject(0);
            
            System.out.println("u3="+u3);
        
        
        } catch (JSONException e) {
            // TODO 自動生成的 catch 塊
            e.printStackTrace();
        }
        
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM