I create some JSONObject using java classes. Lets call it MyJSONObject. It should be the inner String part of another JSON object. Like that:
[
{
"httpRequest": {...},
"httpResponse": {
"headers": {
"content-type": [
"application/json"
]
},
"body": "MyJSONObject"
}
]
The problem is when i try to put it as string using mapper.writeValueAsString(MyJSONObject) I receive all the quotes with backslash in console body :
[
{
"httpRequest": {...},
"httpResponse": {
"headers": {...},
"body": "{\"key\":\"value\",\"key\":\"value\"}"
}
]
instead of expected:
"body": "{"key":"value","key":"value"}"
I tried to replace each quote by quote with backslash, using method replace("\\"", "\\\\\\"")
to my string, but the result is even worse:
"body": "{\\\"key\\\":\\\"value\\\",\\\"key":\\\"value\\\"}"
How can i just put my string avoid this backslashes?
您可以尝试使用以下方法,这会给您带来预期的结果。
JSONObject subJson = new JSONObject(); subJson.put(“name”, “java”);
JSONObject parentJson = new JSONObject() parentJson.put(“company”, ”stack”); parentJson.put(“employees”, subJson);
System.out.print(“Final output - ” + parentJson.toString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.