這篇文章將為大家詳細講解有關JSONObject中的字段如何使用fastjson賦值,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
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); } }