[英]How do I remove backslashes before quotes from a JSONObject string?
I have a list of strings (records) that are dynamically created by a class.我有一个由类动态创建的字符串(记录)列表。 Each record may have different keys (eg
favorite_pizza
on first, favorite_candy
on second).每个记录可以具有不同的密钥(例如
favorite_pizza
上第一, favorite_candy
上秒)。
// Note: These records are dynamically created and not stored
// in this way. This is simply for display purposes.
List<String> records =
Arrays.asList(
"{\"name\":\"Bob\",\"age\":40,\"favorite_pizza\":\"Cheese\"}",
"{\"name\":\"Jill\",\"age\":22,\"favorite_candy\":\"Swedish Fish\"}");
The list of records is then passed to a separate HTTP request class.然后将记录列表传递给单独的 HTTP 请求类。
public Response addRecords(List<String> records) {
...
}
Inside the HTTP request service, I want to build a JSON request body:在 HTTP 请求服务中,我想构建一个 JSON 请求正文:
{
"records": [
{
"name": "Bob",
"age": 40,
"favorite_pizza": "Cheese"
},
{
"name": "Jill",
"age": 22,
"favorite_candy": "Swedish Fish"
}
]
}
I'm using org.json.JSONObject to add the records
key and create the request body:我正在使用org.json.JSONObject添加
records
键并创建请求正文:
JSONObject body = new JSONObject();
// Add the "records" key
body.put("records", records);
// Create the request body
body.toString();
When I run my junit test in IntelliJ, the request body contains a backslash before each quote:当我在 IntelliJ 中运行我的 junit 测试时,请求正文在每个引号之前包含一个反斜杠:
org.junit.ComparisonFailure:
Expected :"{"records":["{"name":"Bob","age":40,"favorite_pizza":"Cheese"}","{"name":"Jill","age":22,"favorite_candy":"Swedish Fish"}"]}"
Actual :"{"records":["{\"name\":\"Bob\",\"age\":40,\"favorite_pizza\":\"Cheese\"}","{\"name\":\"Jill\",\"age\":22,\"favorite_candy\":\"Swedish Fish\"}"]}"
And when I make the request it fails because the body is not formatted correctly:当我发出请求时,它失败了,因为正文格式不正确:
{
"records": [
"{\"name\":\"Bob\",\"age\":40,\"favorite_pizza\":\"Cheese\"}",
"{\"name\":\"Jill\",\"age\":22,\"favorite_candy\":\"Swedish Fish\"}"
]
}
You are creating a list of string, which is not what you want.您正在创建一个字符串列表,这不是您想要的。
You should instead create a list of objects (Maps)您应该创建一个对象列表(地图)
Map<String, Object> m1 = new LinkedHashMap<>();
m1.put("name", "Bob");
m1.put("age", 40);
m1.put("favorite_pizza", "Cheese");
LinkedHashMap<String, Object> m2 = new LinkedHashMap<>();
m2.put("name", "Jill");
m2.put("age", 22);
m2.put("favorite_candy", "Swedish Fish");
List<LinkedHashMap<String, Object>> records = Arrays.asList(m1,m2);
JSONObject body = new JSONObject();
// Add the "records" key
body.put("records", records);
This is a quite common mistake (it seems), to try to serialize strings formatted like json objects expecting is the same thing as passing a the object itself.这是一个很常见的错误(似乎),尝试序列化格式为 json 对象的字符串与传递对象本身是一样的。
UPDATE:更新:
Or if you have a json serialized object list then ...或者,如果您有一个 json 序列化对象列表,那么...
List<String> recordSource =
Arrays.asList(
"{\"name\":\"Bob\",\"age\":40,\"favorite_pizza\":\"Cheese\"}",
"{\"name\":\"Jill\",\"age\":22,\"favorite_candy\":\"Swedish Fish\"}");
List<JSONObject> records =
recordSource.stream().map(JSONObject::new).collect(Collectors.toList());
JSONObject body = new JSONObject();
// Add the "records" key
body.put("records", records);
System.out.println(body.toString());
If your record strings are already valid json you can either如果您的记录字符串已经是有效的 json,您可以
Iterate over them, converting them one at a time into a JSONObject
(see here ) and then add the result to a JSONArray
which you can manipulate if needed.迭代它们,一次将它们转换为
JSONObject
(参见此处),然后将结果添加到JSONArray
,您可以根据需要对其进行操作。
Create the array entirely by hand since it's just comma separated record strings inside square brackets.完全手动创建数组,因为它只是方括号内的逗号分隔记录字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.