一、使用 org.JSON.jsONObject,将第一个json对象转string之后,塞到第二个对象中,会自动加上转义字符。 import org.json.JSONObject;public class Test {public st
一、使用 org.JSON.jsONObject,将第一个json对象转string之后,塞到第二个对象中,会自动加上转义字符。
import org.json.JSONObject;public class Test {public static void main(String[] args) throws Exception {JSONObject json = new JSONObject();json.put("name", "123");JSONObject json2 = new JSONObject();json2.put("name2", "456");json2.put("json", json.toString());// 这里会自动加上转义字符System.out.println(json2);}}
结果:{"json":"{\"name\":\"123\"}","name2":"456"}
二、若是用 net.sf.json.JSONObject,则无论这里 json2.put("json", json.toString()); 有没有转string,都会以json对象的形式。
import net.sf.json.JSONObject;public class Test {public static void main(String[] args) throws Exception {JSONObject json = new JSONObject();json.put("name", "123");JSONObject json2 = new JSONObject();json2.put("name2", "456");json2.put("json", json);System.out.println(json2); json2.put("json", json.toString());System.out.println(json2);}}
结果:{"name2":"456","json":{"name":"123"}}
{"name2":"456","json":{"name":"123"}}
来源地址:https://blog.csdn.net/h18300103614/article/details/128862739
--结束END--
本文标题: json中增加转义字符“\“
本文链接: https://www.lsjlt.com/news/401255.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0