這篇文章將為大家詳細(xì)講解有關(guān)JSONObject中的字段如何使用fastjson賦值,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
JSONObject對(duì)同一個(gè)key重新put時(shí),新值就會(huì)取代舊值,沒有set之類的方法
構(gòu)建json string時(shí),所有的引號(hào)都要轉(zhuǎn)義
package xx; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; // 對(duì)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); } }