001.FastJson常用功能使用

文章来自转载和官方文档,但是代码都是经过重新整理和说明

1.构造JSON格式的数据

package com.shxt.test01;

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shxt.model.User;

public class SimpleTest {
    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();  
        jsonObject.put("key1", "value1"); // String  
        jsonObject.put("key2", 1); // int  
        jsonObject.put("key3", 35.7); // double  
        jsonObject.put("numberString", "55.8");
        
        //---
        Map map = new HashMap();  
        map.put("map_key1", "map_value1");  
        map.put("map_key2", "map_value2"); 
        
        jsonObject.put("key4", map);// 如果是Map,则添加里层JSONObject
        
        jsonObject.putAll(map);// 如果是通过putAll,则会循环依次作为Object对象插入JSONObject数据
        
        User user = new User();  
        user.setAge(18);  
        user.setName("胖先森");
        //如果是实体类对象,则会将实体类组装成JSONObject放进去
        jsonObject.put("user", user);
        
        List users = new ArrayList();  
        for (int i = 0; i < 3; i++) {  
            User u = new User();  
            u.setAge(i + 18);  
            u.setSex(i % 2 == 0 ? "男" : "女");  
            users.add(u);  
        } 
        
        jsonObject.put("list", users); // List 
        
        // 构造JSONArray格式的数据  
        JSONArray array = new JSONArray();  
        array.add("d");  
        jsonObject.put("array", array);
        
        System.out.println(jsonObject.toJSONString());
        
        
    }
}

运行结果为

001.FastJson常用功能使用_第1张图片
运行结果

2.解析JSON格式的数据

就以上面创建的JSON作为源数据进行解析:

{"key1":"value1","key2":1,"numberString":"55.8","array":["d"],"key3":35.7,"key4":{"map_key1":"map_value1","map_key2":"map_value2"},"list":[{"age":18,"sex":"男"},{"age":19,"sex":"女"},{"age":20,"sex":"男"}],"map_key1":"map_value1","user":{"age":18,"name":"胖先森"},"map_key2":"map_value2"}
package com.shxt.test01;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.shxt.model.User;

public class Simple2Test {
    public static void main(String[] args) {
        String source = "{\"key1\":\"value1\",\"key2\":1,\"numberString\":\"55.8\",\"array\":[\"d\"],\"key3\":35.7,\"key4\":{\"map_key1\":\"map_value1\",\"map_key2\":\"map_value2\"},\"list\":[{\"age\":18,\"sex\":\"男\"},{\"age\":19,\"sex\":\"女\"},{\"age\":20,\"sex\":\"男\"}],\"map_key1\":\"map_value1\",\"user\":{\"age\":18,\"name\":\"胖先森\"},\"map_key2\":\"map_value2\"}";
        
        JSONObject jsonObject = new JSONObject();
        
        jsonObject= jsonObject.parseObject(source);
        
        System.out.println("JSONObject : " + jsonObject.toJSONString());  
        
        // key4  
        JSONObject key4Object = jsonObject.getJSONObject("key4");  
        System.out.println("map_key1 value " + key4Object.getString("map_key1"));  
        System.out.println("map_key2 value " + key4Object.getString("map_key2"));  
        // key3  
        System.out.println("key3 value " + jsonObject.getDoubleValue("key3"));  
        System.out.println("key3 value " + jsonObject.getDouble("key3"));  
        System.out.println("key3 value " + jsonObject.getString("key3"));  
        // map_key1  
        System.out.println("map_key1 value " + jsonObject.getString("map_key1"));  
        // key2  
        System.out.println("key2 value " + jsonObject.getInteger("key2"));  
        System.out.println("key2 value " + jsonObject.getIntValue("key2"));  
        System.out.println("key2 value " + jsonObject.getDouble("key2"));  
        System.out.println("key2 value " + jsonObject.getDoubleValue("key2"));  
        System.out.println("key2 value " + jsonObject.getString("key2"));  
        // numberString  
        System.out.println("numberString value " + jsonObject.getString("numberString"));  
        System.out.println("numberString value " + jsonObject.getDouble("numberString"));  
        // user  
        JSONObject userObject = jsonObject.getJSONObject("user");  
        System.out.println("user : " + userObject.toJSONString());  
        User user = JSON.toJavaObject(userObject, User.class);  
        user.setSex("男");  
        System.out.println(user.toString());  
        // JavaBean --> String  
        String userString = JSON.toJSONString(user);  
        System.out.println("userString " + userString);  
        // list  
        JSONArray array = jsonObject.getJSONArray("list");  
        for (int i = 0; i < array.size(); i++) {  
            User user2 = JSON.toJavaObject(array.getJSONObject(i), User.class);  
            System.out.println(user2.toString());  
        }  
        // 解析失败  
        System.out.println("not exit : " + jsonObject.getInteger("integer")); // null  
        System.out.println("not exit : " + jsonObject.getIntValue("integer")); // 0  
        
    }
}

1.如果存入的String类型的数字,在解析的时候同样可以通过getDouble()的方式获取,会自动转换为double格式
2.getXX()方式解析的时候,如果解析失败会返回null,而通过getXXValue()方式解析的时候,如果解析失败,会返回对应方式的默认值,比如:getIntValue() --> 0; getDoubleValue() --> 0.0;

附录 User.java

public class User {
    private Integer age;
    private String name;
    private String sex;
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "User [age=" + age + ", name=" + name + ", sex=" + sex + "]";
    }
    
    
    

}

你可能感兴趣的:(001.FastJson常用功能使用)