java 转义符原值_java去掉json中的转义字符;更改jsonkey的值,保证原值不变。

本文探讨了Java处理JSON转义字符的问题,通过Fastjson库展示了如何在不覆盖原有值的情况下更改JSON对象的键值。通过示例代码,解释了`JSONObject.put()`方法的工作原理,并提供了解决方案,确保在更新JSON对象时不丢失原值。
摘要由CSDN通过智能技术生成

具体情况具体分析,我遇到的情况是这样的:

在put中传参是字符串:

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

public class module1 {

public static void main(String[] args) {

String jsonStr = "{\"username\":\"宋发元\",\"password\":\"123456\",\"age\":\"24\",\"class\":{\"a\":12,\"b\":3}}";

JSONObject j=JSON.parseObject(jsonStr);

String s="{\"a\":12,\"b\":3}";

j.put("class1",s);

System.out.println(j.toJSONString());

}

}

结果是这样的:5e03525bebdad3f8ffa896fe7544e737.pngclass1的 值带\转义字符

然后将String转换为json格式put进去

public static void main(String[] args) {

String jsonStr = "{\"username\":\"宋发元\",\"password\":\"123456\",\"age\":\"24\",\"class\":{\"a\":12,\"b\":3}}";

JSONObject j=JSON.parseObject(jsonStr);

String s="{\"a\":12,\"b\":3}";

JSONObject a=JSON.parseObject(s);

j.put("class1",a);

System.out.println(j.toJSONString());

输出:

e9bc59adc32a40e38192b3a775d8954d.png

转义字符消失了。

首先,put函数的作用是:如果当前key存在则覆盖value值,如果不存在就创建key并存入value值。

如下:

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

public class mod2 {

public static void main(String[] args) {

String jsonStr = "{\"username\":\"宋发元\",\"password\":\"123456\",\"age\":\"24\",\"class\":{\"a\":12,\"b\":3}}";

JSONObject json= JSON.parseObject(jsonStr);

String s="{\"a\":12,\"b\":3}";

String s1="{\"c\":21}";

JSONObject a=JSON.parseObject(s);

JSONObject b=JSON.parseObject(s1);

System.out.println("没有调用put函数的时候json的值为:" + json.toJSONString());

json.put("class1",a);//put函数就是覆盖class1中的值,如果没有class1就创建一个。

System.out.println("调用put函数,但class1不存在的时候json的值为:" + json.toJSONString());

json.put("class",b);//当class中有值的时候,就覆盖掉当前值。

System.out.println("调用put函数,但class存在的时候json的值为:" + json.toJSONString());

}

}

没有调用put函数的时候json的值为:{"username":"宋发元","age":"24","class":{"b":3,"a":12},"password":"123456"}

调用put函数,但class1不存在的时候json的值为:{"username":"宋发元","class1":{"b":3,"a":12},"age":"24","class":{"b":3,"a":12},"password":"123456"}

调用put函数,但class存在的时候json的值为:{"username":"宋发元","class1":{"b":3,"a":12},"age":"24","class":{"c":21},"password":"123456"}

Process finished with exit code 0

从这里可以看出put的作用

现在,更改class的值但不覆盖,我用了一个笨办法

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

public class mod2 {

public static void main(String[] args) {

String jsonStr = "{\"username\":\"宋发元\",\"password\":\"123456\",\"age\":\"24\",\"class\":{\"a\":12,\"b\":3}}";

JSONObject json= JSON.parseObject(jsonStr);

String s="{\"a\":12,\"b\":3}";

String s1="{\"c\":21}"; StringBuffer s2 = new StringBuffer(json.getJSONObject("class").toJSONString());

System.out.println("获取class的值并将json格式转化为String" + s2);

s2.replace(s2.length()-1,s2.length(),",\"d\":22}");

System.out.println("输出替换后的字符串" + s2);

JSONObject json1=JSON.parseObject(s2.toString());

//parseObject函数的参数必须是String我用Stringbuffer只是为了替换简单

json.put("class",json1);

System.out.println("更改后的json内容" + json.toJSONString());

}

}

6c1ebdd28839456c7662ba152de63f8c.png

更改成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值