欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-15 12:00:59
jsonobject对同一个key重新put时,新值就会取代旧值,没有set之类的方法构建json string时,所有的引号都要转义package xx; import com.alibaba.fa...

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中的指定字段重新赋值的实现的文章就介绍到这了,更多相关fastjson jsonobject重新赋值内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!