简体   繁体   English

Java:如何将Json格式的字符串分配给Java字符串?

[英]Java : How to assign Json formated String to Java String?

I have a big json string which i will be getting as a request from the UI , which will be converted to a String and parsed . 我有一个很大的json字符串,我将根据UI的请求获取该字符串,该字符串将转换为String并进行解析。

I want to simulate the similar environment for testing locally , so for this purpose i captured the JSon format. 我想模拟类似的环境以进行本地测试,因此为此目的,我捕获了JSon格式。

Currently i am manually adding "/" to this big json string . 目前,我正在手动将此大json字符串添加“ /”。

Is there any other way to achieve this ?? 还有其他方法可以实现这一目标吗?

For example i got this json 例如我得到了这个json

{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"Preethi"}

and converted that into 并将其转换为

 String str = "{\"age\":\"29\",\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"],\"name\":\"mkyong\"}";

Is there any other way to achieve this ?? 还有其他方法可以实现这一目标吗?

在客户端,进行搜索并在实际发送请求之前,将所需的双引号“正则表达式”全部“替换”为单引号。

Actually, Java doesn't have verbatim string literals . 实际上,Java没有逐字字符串

If you want a Java-like (and Java-VM-based) language that does , however, you might want to look at Groovy which has various forms of string literal . 但是,如果要使用类似Java(和基于Java-VM的语言)的语言, 可能需要查看Groovy ,它具有各种形式的字符串常量

we have in build method to convert jsonObject to string. 我们在build方法中将jsonObject转换为字符串。 Why don't you use that. 你为什么不使用它。

 JSONObject json = new JSONObject();
 json.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM