如何删除通过Javax创建的JsonObject中多余的转义引号字符。

huangapple go评论74阅读模式
英文:

How to remove extra escaping quote characters of JsonObject created through Javax

问题

我使用javax从我的List<String>创建JsonObjectJsonArray,我有一个Json对象的列表,我想通过JsonArray将它们放入JsonObject

JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (String obj : listOfJsonDfObjects)
   jsonArray.add(summaryObj);   //{&quot;a&quot;:&quot;b&quot;},{&quot;c&quot;:&quot;d&quot;}

// 这一行引入了额外的转义引号,就像这样 {&quot;\&quot;a\&quot;&quot;:&quot;\&quot;b\&quot;&quot;},{&quot;\&quot;c\&quot;&quot;:&quot;\&quot;d\&quot;&quot;}
javax.json.JsonObject data = Json.createObjectBuilder()
   .add(&quot;data&quot;, jsonArray.build()).build();

如何避免这些额外的引号转义字符?
谢谢。

英文:

I use javax to create JsonObject and JsonArray from my List&lt;String&gt; 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);   //{&quot;a&quot;:&quot;b&quot;},{&quot;c&quot;:&quot;d&quot;}
   
    // this line introduces extra escaping quotes like this {&quot;\&quot;a\&quot;&quot;:&quot;\&quot;b\&quot;&quot;},{&quot;\&quot;c\&quot;&quot;:&quot;\&quot;d\&quot;&quot;}
    javax.json.JsonObject data = Json.createObjectBuilder()
       .add(&quot;data&quot;, 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&lt;String&gt; listOfJsonDfObjects = List.of(
                &quot;{\&quot;a\&quot;:\&quot;b\&quot;}&quot;,
                &quot;{\&quot;c\&quot;:\&quot;d\&quot;}&quot;
        );
        JsonArrayBuilder jsonArray = Json.createArrayBuilder();
        for (String summaryObj : listOfJsonDfObjects) {
            JsonReader parser = Json.createReader(new StringReader(summaryObj));
            jsonArray.add(parser.readObject());
        }

        JsonObject data = Json.createObjectBuilder()
                .add(&quot;data&quot;, jsonArray.build()).build();

        System.out.println(data); // {&quot;data&quot;:[{&quot;a&quot;:&quot;b&quot;},{&quot;c&quot;:&quot;d&quot;}]}
    }
}

答案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(&quot;\\\\&quot;, &quot;&quot;);
    json = json.replaceAll(&quot;\&quot;\\{&quot;, &quot;{&quot;);
    json = json.replaceAll(&quot;\\}\&quot;&quot;, &quot;}&quot;);        
    //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(&quot;,&quot;, listOfJsonDfObjects);
    //convert to pseudo array
    json = &quot;[&quot; + json + &quot;]&quot;; 
    //convert pseudo json array to pseudo json object
    json = &quot;{\&quot;data\&quot;:&quot; + json + &quot;}&quot;;
    //cast to json object
    JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);                       
    System.out.println(jsonObject);

huangapple
  • 本文由 发表于 2023年2月16日 03:07:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定