首页 > 编程开发 > Java    日期:2020-11-23 / 浏览

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);
  }
}

fastjson对JSONObject中的指定字段重新赋值的实现

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章