首页 > 技术文章 > {"aa":null} 如何能转化为 {"aa":{}}

dawabigbaby 2019-12-12 10:24 原文

一个同事问的一个功能需求:
{"aa":null} 如何能转化为 {"aa":{}}
因为需求暂时不明确,暂时先完成这样的转换。
使用的是FastJson1.2.7

package com.dawa.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @Title: TestJsonTrans
 * @Author: 大娃
 * @Date: 2019/12/12 10:02
 * @Description: {"aa":null}  如何能转化为 {"aa":{}}
 */
public class TestJsonTrans {

    public static void main(String[] args) {
        String jsons = "{\"aa\":null}";
        JSONObject query = JSON.parseObject(jsons);
        //上面先模拟一个json对象。然后进行处理
        JSONObject aa = query.getJSONObject("aa");
        System.out.println(aa);
        if (null == aa) {
            query.put("aa", new JSONObject());
        }
        System.out.println(query);
    }
}

推荐阅读