java解析json数据

1、json的依赖包
json的依赖包共有6个:json-lib.jar、ezmorph.jar、commons-lang.jar、commons-beanutils.jar、commons-collections.jar、commons-logging.jar

2、下载地址
(1)json-lib-2.4-jdk15.jar
https://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/
(2)ezmorph-1.0.6.jar
https://sourceforge.net/projects/ezmorph/files/ezmorph/ezmorph-1.0.6/
(3)commons-lang-2.6.jar
http://commons.apache.org/proper/commons-lang/download_lang.cgi
(4)commons-beanutils-1.9.3.jar
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
(5)commons-collections-3.2.2.jar
http://commons.apache.org/collections/download_collections.cgi
(6)commons-logging-1.2.jar
http://commons.apache.org/proper/commons-logging/download_logging.cgi
注意:上述jar版本能够解析数据,如果换了其他版本可能会出现问题,如果需要相应的jar包可以从下述的github下载。

3、Json解析实例
github地址:
https://github.com/MasonYyp/analysisJson.git

//实体

package analysisJson;

public class Person {
    public String name = null;
    public int age = 0;
    public String sex = null;
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    public int getAge() {
        return this.age;
    }
    
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getSex() {
        return this.sex;
    }
    
}


//解析
package analysisJson;

import java.util.ArrayList;

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

public class AnalysisJson {
    private String data = null;
    
    private void setData(String data) {
        this.data = data;        
    }
    private String getData() {
        return this.data;
    }
    
    // Analysis data of json
    public void analysisData(String data) {
        //new object of json
        JSONObject jsonObject = new JSONObject();
        jsonObject = JSONObject.fromObject(data);
        JSONArray persons = jsonObject.getJSONArray("persons");
        
        JSONObject personObject = new JSONObject();
        Person person=new Person();
        for(int i=0; i<persons.size();i++) {
            /*
            // First method, convert entity of class
            JSONObject jsonObjectPerson = JSONObject.fromObject(persons.getString(i));
            person = (Person) JSONObject.toBean(jsonObjectPerson, Person.class);
            System.out.println(person.age);
            */
                        
            // Second method, direct call method
            personObject = persons.getJSONObject(i);
            System.out.println(personObject.getString("name"));
            System.out.println(personObject.getInt("age"));
 
        }        
    }
    
    // Convert the entity of data
    public void convertEntity() {

        String json = "{\"person\":{\"name\":\"zhaoliu\",\"age\":\"30\",\"sex\":\"man\"}}";
        JSONObject jsonObject = JSONObject.fromObject(json);
        //First, you must convert the json to string
        String stringPerson = jsonObject.getString("person");
        //Then, convert the string to JSONObject,otherwise error !
        JSONObject jsonObjectPerson = JSONObject.fromObject(stringPerson);
        Person person = (Person) JSONObject.toBean(jsonObjectPerson, Person.class);
        System.out.println(person.age);
        
    }
    
    // Generate the data of json
    public void generateJson() {
        // Set the list
        ArrayList<Person> persons = new ArrayList<Person>();
        
        Person person = new Person();
        person.setName("sunlin");
        person.setAge(10);
        person.setSex("man");
        persons.add(person);
        
        person.setName("zhaowu");
        person.setAge(20);
        person.setSex("woman");
        persons.add(person);
        
        // Generate the list of json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("persons", persons);
        
        System.out.println(jsonObject.toString());
        
    }
        
    public static void main(String[] args) {
                
        AnalysisJson aj = new AnalysisJson();
        
        //Json data
        String data = "{\"persons\":[{\"name\":\"zhangsan\",\"age\":\"10\",\"sex\":\"man\"},"
                + "{\"name\":\"lisi\",\"age\":\"20\",\"sex\":\"woman\"},"
                + "{\"name\":\"wangwu\",\"age\":\"30\",\"sex\":\"man\"}]}";
        
        // Print the data of json
        System.out.println(data);
        
        // Analysis the data of json
        aj.setData(data);
        aj.analysisData(aj.getData());
        
        /*
        // Convert the entity
        aj.convertEntity();
        */
        
        // Generate the data of json
        aj.generateJson();
                
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值