fastjson對JSONObject中的指定字段重新賦值

JSONObject對同一個key重新put時,新值就會取代舊值,沒有set之類的方法

構建json string時,所有的引號都要轉義

package xx;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
// 對JSONObject中的指定字段重新賦值
public class testJsonReplace {
 
    public static void main(String[] args) {
 
		String query_jsonstr = "{\"timezone\":\"GMT+0\",\"action\":\"front_BRAND\",\"format\":\"true\",\"lan\":\"en_us\",\"column\":[\"day\",\"impressions\",\"clicks\",\"ctr\",\"brand_estimated_ecpm\",\"pay_out\"],\"dimension\":[\"day\"],\"filter\":{\"posid\":{\"op\":\"in\",\"value\":\"2454105\"}},\"start\":1528761600000,\"end\":1529452800000}";
		JSONObject query = JSON.parseObject(query_jsonstr);
 
		if (query.getJSONObject("filter") != null) {
			JSONObject posJson = new JSONObject();
			posJson.put("op", "in");
			posJson.put("value", "2454117");
			query.getJSONObject("filter").put("posid", posJson);
		}
        String posid = query.getJSONObject("filter").getJSONObject("posid").getString("value");
		String newJsonStr = query.toJSONString();
		System.out.println(newJsonStr);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章