JSONObject应用Json字符串和Object对象之间的转换,Map封装数据思路

2024-10-09 11:25:19 浏览数 (1)

JSONObject应用Json字符串和Object对象之间的转换,Map封装数据思路

代码语言:javascript复制
package com.example.core.mydemo.json5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

public class Item {
    private String id;
    @JSONField(serialize = false)  //fastjson 忽略字段打印
    private String rate;

    private String status;

    public Item() {
    }

    public Item(String id, String rate) {
        this.id = id;
        this.rate = rate;
    }

    @Override
    public String toString() {
        return "Item{"  
                "id='"   id   '''  
                ", rate='"   rate   '''  
                ", status='"   status   '''  
                '}';
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * output  {"id":"111"}
     * @param args
     */
    public static void main(String[] args) {
        Item item = new Item();
        item.setId("111");
        item.setRate("56666");
        System.out.println(JSON.toJSONString(item));
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }
}

package com.example.core.mydemo.json5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * output:
 * jsonString={"id":"4"}
 * id=4
 * status=成功
 * item2=Item{id='4', rate='null', status='成功'}
 * item3=Item{id='4', rate='null', status='成功'}
 */
public class JSONObjectTest {
    public static void main(String[] args) {
        Item item = new Item("4","100");
        String jsonString = JSON.toJSONString(item);
        System.out.println("jsonString=" jsonString);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        if (jsonObject.containsKey("id")) {
            String id = jsonObject.getString("id");
            System.out.println("id="    id);
        }
        if (jsonObject.containsKey("rate")) {
            String rate = jsonObject.getString("rate");
            System.out.println("rate="    rate);
        }

        //封装数据
        jsonObject.put("status","成功");
        if (jsonObject.containsKey("status")) {
            String status = jsonObject.getString("status");
            System.out.println("status="    status);
        }
//      T =  JSON.parseObject("json","Class T");
        Item item2 = jsonObject.toJavaObject(Item.class);
        System.out.println("item2="   item2.toString());

        Type type = new TypeToken<Item>() {}.getType();
        Item item3 = jsonObject.toJavaObject(type);
        System.out.println("item3="   item3.toString());

    }
}
代码语言:javascript复制
package com.example.core.mydemo.json5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

public class Item {
    private String id;
    @JSONField(serialize = false)  //fastjson 忽略字段打印
    private String rate;

    private String status;

    public Item() {
    }

    public Item(String id, String rate) {
        this.id = id;
        this.rate = rate;
    }

    @Override
    public String toString() {
        return "Item{"  
                "id='"   id   '''  
                ", rate='"   rate   '''  
                ", status='"   status   '''  
                '}';
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * output  {"id":"111"}
     * @param args
     */
    public static void main(String[] args) {
        Item item = new Item();
        item.setId("111");
        item.setRate("56666");
        System.out.println(JSON.toJSONString(item));
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }
}

package com.example.core.mydemo.json5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * output:
 * jsonString={"id":"4"}
 * id=4
 * status=成功
 * item2=Item{id='4', rate='null', status='成功'}
 * item3=Item{id='4', rate='null', status='成功'}
 */
public class JSONObjectTest {
    public static void main(String[] args) {
        Item item = new Item("4","100");
        String jsonString = JSON.toJSONString(item);
        System.out.println("jsonString=" jsonString);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        if (jsonObject.containsKey("id")) {
            String id = jsonObject.getString("id");
            System.out.println("id="    id);
        }
        if (jsonObject.containsKey("rate")) {
            String rate = jsonObject.getString("rate");
            System.out.println("rate="    rate);
        }

        //封装数据
        jsonObject.put("status","成功");
        if (jsonObject.containsKey("status")) {
            String status = jsonObject.getString("status");
            System.out.println("status="    status);
        }
//      T =  JSON.parseObject("json","Class T");
        Item item2 = jsonObject.toJavaObject(Item.class);
        System.out.println("item2="   item2.toString());

        Type type = new TypeToken<Item>() {}.getType();
        Item item3 = jsonObject.toJavaObject(type);
        System.out.println("item3="   item3.toString());

    }
}

0 人点赞