如何使用fastjson对JSONObject的特定字段进行重新赋值操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计249个文字,预计阅读时间需要1分钟。
JSONObject 对同一个 key 重新 put 时,新值会取代旧值。没有 set 类型的函数 + 构建 json string 时,所有的引用都要转换,例如:
javapackage xx;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;
public class Main { public static void main(String[] args) { JSONObject jsonObject=new JSONObject(); jsonObject.put(key, value1); jsonObject.put(key, value2); // value1 被value2取代 String jsonString=JSON.toJSONString(jsonObject); System.out.println(jsonString); }}
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中的指定字段重新赋值的实现的文章就介绍到这了,更多相关fastjson JSONObject重新赋值内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计249个文字,预计阅读时间需要1分钟。
JSONObject 对同一个 key 重新 put 时,新值会取代旧值。没有 set 类型的函数 + 构建 json string 时,所有的引用都要转换,例如:
javapackage xx;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;
public class Main { public static void main(String[] args) { JSONObject jsonObject=new JSONObject(); jsonObject.put(key, value1); jsonObject.put(key, value2); // value1 被value2取代 String jsonString=JSON.toJSONString(jsonObject); System.out.println(jsonString); }}
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中的指定字段重新赋值的实现的文章就介绍到这了,更多相关fastjson JSONObject重新赋值内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

