英文:
How to remove extra escaping quote characters of JsonObject created through Javax
问题
我使用javax从我的List<String>创建JsonObject
和JsonArray
,我有一个Json对象的列表,我想通过JsonArray
将它们放入JsonObject
中
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (String obj : listOfJsonDfObjects)
jsonArray.add(summaryObj); //{"a":"b"},{"c":"d"}
// 这一行引入了额外的转义引号,就像这样 {"\"a\"":"\"b\""},{"\"c\"":"\"d\""}
javax.json.JsonObject data = Json.createObjectBuilder()
.add("data", jsonArray.build()).build();
如何避免这些额外的引号转义字符?
谢谢。
英文:
I use javax to create JsonObject
and JsonArray
from my List<String>
and I have a list of Json objects that i want to put in a JsonObject
through a JsonArray
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (String Obj : listOfJsonDfObjects)
jsonArray.add(summaryObj); //{"a":"b"},{"c":"d"}
// this line introduces extra escaping quotes like this {"\"a\"":"\"b\""},{"\"c\"":"\"d\""}
javax.json.JsonObject data = Json.createObjectBuilder()
.add("data", jsonArray.build()).build();
How to avoid these extra quotes escaping characters?
Thanks
答案1
得分: 1
你说你有一个JSON对象列表,但实际上你有一个JSON格式的字符串列表。要将它们添加到JsonArray
中,你需要将每个字符串解析为JSON对象模型:
public class JsonTest {
public static void main(String[] args) {
List<String> listOfJsonDfObjects = List.of(
"{\"a\":\"b\"}",
"{\"c\":\"d\"}"
);
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (String summaryObj : listOfJsonDfObjects) {
JsonReader parser = Json.createReader(new StringReader(summaryObj));
jsonArray.add(parser.readObject());
}
JsonObject data = Json.createObjectBuilder()
.add("data", jsonArray.build()).build();
System.out.println(data); // {"data":[{"a":"b"},{"c":"d"}}
}
}
英文:
You say you have a list of JSON objects, but you really have a list of JSON-formatted strings. To add them to a JsonArray
, you need to parse each one into the JSON object model:
public class JsonTest {
public static void main(String[] args) {
List<String> listOfJsonDfObjects = List.of(
"{\"a\":\"b\"}",
"{\"c\":\"d\"}"
);
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (String summaryObj : listOfJsonDfObjects) {
JsonReader parser = Json.createReader(new StringReader(summaryObj));
jsonArray.add(parser.readObject());
}
JsonObject data = Json.createObjectBuilder()
.add("data", jsonArray.build()).build();
System.out.println(data); // {"data":[{"a":"b"},{"c":"d"}]}
}
}
答案2
得分: -1
使用Gson
Gson gson = new Gson();
String json = gson.toJson(listOfJsonDfObjects);
// 检查 JSON
System.out.println(json);
json = json.replaceAll("\\\\", "");
json = json.replaceAll("\\\"\\{", "{");
json = json.replaceAll("\\}\\\"\"", "}");
// 现在是有效的 JSON
System.println.println(json);
更安全的方法(避免修改原始数据)
// 将列表中的对象用逗号连接起来
String json = String.join(",", listOfJsonDfObjects);
// 转换为伪数组
json = "[" + json + "]";
// 转换伪 JSON 数组为伪 JSON 对象
json = "{\"data\":" + json + "}";
// 转换为 JSON 对象
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
System.out.println(jsonObject);
英文:
Using Gson
Gson gson = new Gson();
String json = gson.toJson(listOfJsonDfObjects);
//check json
System.out.println(json);
json = json.replaceAll("\\\\", "");
json = json.replaceAll("\"\\{", "{");
json = json.replaceAll("\\}\"", "}");
//valid json now
System.out.println(json);
A more secure way (to avoid altering original data)
//concatenate objects in list with comma
String json = String.join(",", listOfJsonDfObjects);
//convert to pseudo array
json = "[" + json + "]";
//convert pseudo json array to pseudo json object
json = "{\"data\":" + json + "}";
//cast to json object
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
System.out.println(jsonObject);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论