xoxobool

成功者,永远成功,失败者,永远失败,我要面对者,走向成功!

导航

JSON解析

1.JSON概念

(Java   Script   Object  Notation)是一种轻量级的数据交换格式,其本质是一种特定的字符串格式。

2.规则

集合[]和对象{}两种

显示方式为:[value1,value2],{key1:value1,key2:value2}

[{key1:value1},{key2:value2}...]{key1(字符串):[value1]}

3.解析

JSON解析有解析工具,可以分为好多,这里就说两种官方JSON解析jar包和阿里巴巴的JSON解析jar包

JSON解析形式上分为两种:

【1】.从对象到(集合)JSON字符串

【2】.从JSON字符串到对象(集合)

具体操作代码如下,导入jar包的方法就不再写了

1.先有一个实体类为后面测试JSON解析做准备

package com.hanqi.test;

import java.util.Date;

public class User {

    private int userID;
    private String userName;
    private String password;
    private Date birthday;
    
    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 Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public User(int userID, String userName, String password) {
        super();
        this.userID = userID;
        this.userName = userName;
        this.password = password;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "User [userID=" + userID + ", userName=" + userName + ", password=" + password + ", birthday=" + birthday
                + "]";
    }

}

2.测试代码如下:

package com.hanqi.test;

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

import org.json.JSONArray;
import org.json.JSONException;

import com.alibaba.fastjson.JSONObject;

public class TestJson {

    
    public static void main(String[] args)
    {
        
        //测试JSON解析
        
        // 1 从对象到(集合)JSON字符串
        
        User u1 = new User(999,"admin","1234");
        
        u1.setBirthday(new Date());
        
        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"));
        lu.add(new User(555,"User5","111"));
        
        String jlu=com.alibaba.fastjson.JSONArray.toJSONString(lu);
        
        System.out.println("jlu="+jlu);
        
        
        
        
        // 2 从JSON字符串到对象(集合)
        
        User u2=(User)JSONObject.parseObject(ju1, User.class);
        
        System.out.println(u2); 
        
        try {
                org.json.JSONObject jo = new  org.json.JSONObject(ju1);
                
                int userid = jo.getInt("userID");
                
                System.out.println("userID="+userid);
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        // 字符串到集合
        List<User> lu2 = com.alibaba.fastjson.JSONArray.parseArray(jlu, User.class);
        
        for(User u:lu2)
        {
            System.out.println(u);
        }
        
        try {
                JSONArray ja = new JSONArray(jlu);
                
                org.json.JSONObject u3= ja.getJSONObject(0);
                
                System.out.println("u3="+u3);
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

posted on 2016-11-25 20:02  春之林木  阅读(156)  评论(0编辑  收藏  举报