如何在JSON对象中转义引号? [英] How to escape the quotes in JSON Object?

查看:1390
本文介绍了如何在JSON对象中转义引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的方法,它使 JSONObject ,然后打印出 JSONString



我使用Google GSON。

  private String generateData(ConcurrentMap< String,Map< Integer ,Set< Integer>>> dataTable,int i){

JsonObject jsonObject = new JsonObject();

Set< Integer> ap = dataTable.get(TEST1)。get(i);
Set< Integer> bp = dataTable.get(TEST2)。get(i);

jsonObject.addProperty(description,test data);
jsonObject.addProperty(ap,ap.toString());
jsonObject.addProperty(bp,bp.toString());

System.out.println(jsonObject.toString());

return jsonObject.toString();
}

目前如果我打印出 jsonObject.toString( )然后打印出来 -

  {description:test data, ap:[0,1100,4,1096],bp:[1101,3,6,1098]} 

但这不是我所需要的。我希望打印出如下所示的 ap bp 值不含双引号。



$ p $ {description:test data,ap:[0,1100,4,1096],bp:[1101,3 ,6,1098]}

我不知道如何在JSONObject中转义那些引号?

解决方案

您的问题在于

  jsonObject.addProperty(ap,ap.toString()); 

您正在添加属性 String 在Java中表示 Set 。它与JSON无关(即使格式看起来相同)。



您必须将您的 Set 转换成 JsonElement (a JsonArray 真的但你不会看到)。



在某处创建 Gson 对象

  Gson gson = new Gson(); 

并用它转换你的 Set 元素到 JsonElement 对象,并将它们添加到 JsonObject

  jsonObject.add(ap,gson.toJsonTree(ap)); 
jsonObject.add(bp,gson.toJsonTree(bp));

Gson有其惯例,它将 Set 添加到 JsonArray 中,这是 JsonElement 的子类型,因此您可以将它添加为 JsonObject#add(String,JsonElement)


Below is my method which makes the JSONObject and then print out the JSONString.

I am using Google GSON.

private String generateData(ConcurrentMap<String, Map<Integer, Set<Integer>>> dataTable, int i) {

    JsonObject jsonObject = new JsonObject();

    Set<Integer> ap = dataTable.get("TEST1").get(i);
    Set<Integer> bp = dataTable.get("TEST2").get(i);

    jsonObject.addProperty("description", "test data");
    jsonObject.addProperty("ap", ap.toString());
    jsonObject.addProperty("bp", bp.toString());

    System.out.println(jsonObject.toString());

    return jsonObject.toString();
}

Currently if I print out the jsonObject.toString() then it prints out like this -

{"description":"test data","ap":"[0, 1100, 4, 1096]","bp":"[1101, 3, 6, 1098]"}

But this is not what I need. I want to print out like below which is without double quote on ap and bp values.

{"description":"test data","ap":[0, 1100, 4, 1096],"bp":[1101, 3, 6, 1098]}

I am not sure how do I escape that quotes in the JSONObject?

解决方案

Your problem is that with

jsonObject.addProperty("ap", ap.toString());

you are adding a property which is the String representation of a Set in Java. It has nothing to do with JSON (even if the format looks the same).

You will have to convert your Set into a JsonElement (a JsonArray really but you won't see that).

Create a Gson object somewhere

Gson gson = new Gson();

and use it to convert your Set elements to JsonElement objects and add them to the JsonObject.

jsonObject.add("ap", gson.toJsonTree(ap));
jsonObject.add("bp", gson.toJsonTree(bp));

Gson has its conventions, it converts a Set into a JsonArray which is a sub type of JsonElement and you can therefore add it with JsonObject#add(String, JsonElement).

这篇关于如何在JSON对象中转义引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆