首页 > 代码库 > Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)

Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)

      

    首先声明一下,jsonlib转换与GSON相比太差劲了,操作不是一般的繁琐。GSON可以直接转换成各种集合与对象类型。强烈推荐使用GSON。而且GSON一个方法就可以解决,jsonlib转来转去太繁琐了。

手动创建JSONObject与JSONArray有用,用于读取文件对字符串进行处理

 

-----------------------------jsonlib操作复杂,转换Map与list<map>更是复杂----------------

 

Jar包

技术分享

User.java

 1 package TestJson_JSONlib;
 2 
 3 public class User {
 4 
 5     private String id;
 6     private String name;
 7     private Integer age;
 8     private String address;
 9     public String getId() {
10         return id;
11     }
12     public int getAge() {
13         return age;
14     }
15     
16     public void setAge(Integer age) {
17         this.age = age;
18     }
19     public String getAddress() {
20         return address;
21     }
22     public void setAddress(String address) {
23         this.address = address;
24     }
25     public void setId(String id) {
26         this.id = id;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public User() {
35     }
36     public User(String id, String name, Integer age, String address) {
37         super();
38         this.id = id;
39         this.name = name;
40         this.age = age;
41         this.address = address;
42     }
43     @Override
44     public String toString() {
45         return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
46     }
47     
48     
49     
50     
51 }

 

 1.Java对象转json(操作复杂)

对象类型用JSONObject,集合类型用JSONArray

 

package TestJson_JSONlib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.UnmarshallerHandler;

import org.junit.Test;

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

public class Bean2JSON {

//    测试单个对象转JSON,转单个对象用JSONObject
    @Test
    public void test1(){
        User user = new User("1", "QIAOZHI",25,"山西太原");
        JSONObject jsonObject =  JSONObject.fromObject(user);
        String json = jsonObject.toString();
        System.out.println(json);   
        //{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}
    }
    
    
    
//    测试list转JSON,转集合用JSONArray,会产生[]
    @Test
    public void test2(){
        List<User> list = new ArrayList<User>();
        list.add(new User("1", "QIAOZHI",25,"山西太原"));
        list.add(new User("2", "QIAOZHI",25,"山西太原"));
        list.add(new User("3", "QIAOZHI",25,"山西太原"));
        JSONArray jsonArray =  JSONArray.fromObject(list);
        String json = jsonArray.toString();
        System.out.println(json);
//        [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}]
    }
    
//    测试map对象转JSON,转单个对象用JSONObject
    @Test
    public void test3(){
        Map<String,User> map = new HashMap<String,User>();
        map.put("1", new User("1", "QIAOZHI",25,"山西太原"));
        map.put("2", new User("2", "QIAOZHI",25,"山西太原"));
        map.put("3", new User("3", "QIAOZHI",25,"山西太原"));
        JSONObject jsonObject=  JSONObject.fromObject(map);
        String json = jsonObject.toString();
        System.out.println(json);
//        {"3":{"address":"山西太原","age":25,"id":"3","name":"QIAOZHI"},"2":{"address":"山西太原","age":25,"id":"2","name":"QIAOZHI"},"1":{"address":"山西太原","age":25,"id":"1","name":"QIAOZHI"}}
    }
    
//    测试list<map>对象转JSON,转集合用JSONArray,会产生[]
    @Test
    public void test4(){
        List<Map<String,User>> list = new ArrayList<>();
        Map<String,User> map1 = new HashMap<String,User>();
        Map<String,User> map2 = new HashMap<String,User>();
        map1.put("map11", new User("1", "QIAOZHI",25,"山西太原"));
        map1.put("map12", new User("2", "QIAOZHI",25,"山西太原"));
        map2.put("21", new User("11", "QIAOZHI",25,"山西太原"));
        map2.put("22", new User("21", "QIAOZHI",25,"山西太原"));
        list.add(map1);
        list.add(map2);
        JSONArray jsonArray =  JSONArray.fromObject(list);
        String json = jsonArray.toString();
        System.out.println(json);
//        [{"map12":{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},"map11":{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}},{"21":{"id":"11","name":"QIAOZHI","age":25,"address":"山西太原"},"22":{"id":"21","name":"QIAOZHI","age":25,"address":"山西太原"}}]
    }
    
}

 

 

2.JSON字符串转Java对象与list(只测了转bean与list)

转出来的中间还有空格,使用时候还得去空格。太繁琐了。

在将 Json 形式的字符串转换为 JavaBean 的时候需要注意 JavaBean 中必须有无参构造函数,否则会报找不到初始化方法的错误。

 

package TestJson_JSONlib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.google.gson.Gson;

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

public class JSON2Bean {
//    测试json转单个对象
    @Test
    public void test1(){
        String json = "{\"id\":\"1\",\"name\":\"QIAOZHI\",\"age\":25,\"address\":\"山西太原\"}";
        JSONObject jsonObject = JSONObject.fromObject(json);
        System.out.println(jsonObject);  //产生一个json格式的json对象
//        {"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}
        User user = (User) JSONObject.toBean(jsonObject, User.class);
        System.out.println(user);  
//        User [id=1, name=QIAOZHI, age=null, address=山西太原]
    }
    
    
    
//    测试JSON转list(太反复杂了)
    @SuppressWarnings("unchecked")
    @Test
    public void test2(){
        String json = "[{‘id‘:‘1‘,‘name‘:‘QIAOZHI‘,‘age‘:25,‘address‘:‘山西太原‘},{‘id‘:‘2‘,‘name‘:‘QIAOZHI‘,‘age‘:25,‘address‘:‘山西太原‘},{‘id‘:‘3‘,‘name‘:‘QIAOZHI‘,‘age‘:25,‘address‘:‘山西太原‘}]";
        List<User> list = new ArrayList<User>();
        JSONArray jsonArray = JSONArray.fromObject(json);
        System.out.println(jsonArray);   //产生一个JSON格式的数组
//        [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}]
        list = (List<User>) JSONArray.toList(jsonArray,User.class);
        System.out.println(list);
//        [User [id=1, name=QIAOZHI, age=null, address=山西太原], User [id=2, name=QIAOZHI, age=null, address=山西太原], User [id=3, name=QIAOZHI, age=null, address=山西太原]]
        System.out.println(list.get(1));
//        User [id=2, name=QIAOZHI, age=null, address=山西太原]
    }
}

 

 

 

3.手动创建JSONObject与JSONArray(用于JSON转java对象与List集合)

 

package TestJson_JSONlib;

import org.junit.Test;

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

public class CreateJSONObject {

    @Test
    public void test1(){
//        手动创建一个JSONObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", "11");
        jsonObject.put("name", "qiaozhi");
        jsonObject.put("age", 30);
        jsonObject.put("address", "山西太原");
        System.out.println(jsonObject);
//        {"id":"11","name":"qiaozhi","age":30,"address":"山西太原"}
        
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("id", "12");
        jsonObject1.put("name", "qiaozhi");
        jsonObject1.put("age", 30);
        jsonObject1.put("address", "山西太原");
        System.out.println(jsonObject1);
//        {"id":"12","name":"qiaozhi","age":30,"address":"山西太原"}
        
        
//        产生一个JSONArray
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        System.out.println(jsonArray);
//        [{"id":"11","name":"qiaozhi","age":30,"address":"山西太原"},{"id":"12","name":"qiaozhi","age":30,"address":"山西太原"}]
        
        
//        用上述方法转成Java对象与List集合,用于处理数据
    }
}

 

 

 

 

 

 

 

//    测试json转单个对象
    @Test
    public void test1(){
        String json = "{\"id\":\"1\",\"name\":\"QIAOZHI\",\"age\":25,\"address\":\"山西太原\"}";
        JSONObject jsonObject = JSONObject.fromObject(json);
        System.out.println(jsonObject);  //产生一个json格式的json对象
//        {"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}
        User user = (User) JSONObject.toBean(jsonObject, User.class);
        System.out.println(user);  
//        User [id=1, name=QIAOZHI, age=null, address=山西太原]
    }
    
    
    
//    测试JSON转list(太反复杂了)
    @SuppressWarnings("unchecked")
    @Test
    public void test2(){
        String json = "[{‘id‘:‘1‘,‘name‘:‘QIAOZHI‘,‘age‘:25,‘address‘:‘山西太原‘},{‘id‘:‘2‘,‘name‘:‘QIAOZHI‘,‘age‘:25,‘address‘:‘山西太原‘},{‘id‘:‘3‘,‘name‘:‘QIAOZHI‘,‘age‘:25,‘address‘:‘山西太原‘}]";
        List<User> list = new ArrayList<User>();
        JSONArray jsonArray = JSONArray.fromObject(json);
        System.out.println(jsonArray);   //产生一个JSON格式的数组
//        [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}]
        list = (List<User>) JSONArray.toList(jsonArray,User.class);
        System.out.println(list);
//        [User [id=1, name=QIAOZHI, age=null, address=山西太原], User [id=2, name=QIAOZHI, age=null, address=山西太原], User [id=3, name=QIAOZHI, age=null, address=山西太原]]
        System.out.println(list.get(1));
//        User [id=2, name=QIAOZHI, age=null, address=山西太原]
    }

Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)