标题翻译
how to print nested json-object without escapes and quotation marks
问题
我有一个名为 jsonObject
的 JSON 对象
{
"action":"Read",
"infos":[
{
"value":0.0350661,
"key":"first"
}
]
}
我想以以下形式打印 JSON 对象
{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}
如果我使用 jsonObject.toString()
方法,我会得到
{"action":"Read","infos":"[{\"value\":0.0350661,\"key\":\"first\"}]"}
如果我使用 StringEscapeUtils.unescapeJava(jsonObject.toString())
方法,我会得到
{"action":"Read","infos":"[{\"value\":0.0350661,\"key\":\"first\"}]"}
如果我使用 Jackson Mapper,以下是代码示例
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
String jsonString = mapper.writeValueAsString(getDebugInfo());
我会得到 jsonString
如下
{"nameValuePairs":{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}}
是否有解决方案以获得所需的输出 JSON 字符串?
英文翻译
i have a json-object named jsonObject
{
"action":"Read",
"infos":[
{
"value":0.0350661,
"key":"first"
}
]
}
i wanna to print the json-object to with the following form
{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}
if i use jsonObject.toString()
method i will get
{"action":"Read","infos":"[{\"value\":0.0350661,\"key\":\"first\"}]"}
if i use StringEscapeUtils.unescapeJava(jsonObject.toString())
method i will get
{"action":"Read","infos":"[{"value":0.0350661,"key":"first"}]"}
if i use jackson mapper with the following code
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
String jsonString = mapper.writeValueAsString(getDebugInfo())
i will get jsonString
as
{"nameValuePairs":{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}}
is there any solution to get the desired output json-string?
答案1
得分: 0
- JSON结构
您将其作为一个对象,所以在那里没有引号。
在您的示例中,存在一个数组对象,在Json结构中。
- 代码/Java
在控制台打印时,引用了json正文的每个键和值的toString()。
这就是为什么双引号存在,因为正在使用字符串!
英文翻译
- JSON Structure
You have that as an object, that is why quotes are not present there.
In your example, an array object is present, at the Json structure.
- Code/Java
While printing at Console, the json body's every Key & Value toString() are referred .
That is why the Double Quotes present, as Strings are getting used!
答案2
得分: 0
这里我尝试使用GSON库编写了以下代码,并且打印出了与上面所示相同的正确JSON。
public static void main(String[] args) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action", "Read");
JsonArray jsonArr = new JsonArray();
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("value", 0.0350661);
jsonObject2.addProperty("key", "first");
jsonArr.add(jsonObject2);
jsonObject.add("infos", jsonArr);
String jsonString = jsonObject.toString();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement json = gson.fromJson(jsonString, JsonElement.class);
String jsonInString = gson.toJson(json);
System.out.println(jsonInString);
}
输出:
{
"action": "Read",
"infos": [
{
"value": 0.0350661,
"key": "first"
}
]
}
即使我使用org.json
库构建了jsonObject
,然后在控制台上使用System.out.println(jsonObject.toString());
进行简单打印,我得到的结果也是这样的:
{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}
因此,在这里,我不确定您是如何构建您的jsonObject
的。
英文翻译
Here I have tried the below code using GSON library, and it is printing me the correct json as shown above.
public static void main ( String [] args ) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action", "Read");
JsonArray jsonArr = new JsonArray();
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("value", 0.0350661);
jsonObject2.addProperty("key", "first");
jsonArr.add(jsonObject2);
jsonObject.add("infos", jsonArr);
String jsonString = jsonObject.toString();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement json = gson.fromJson(jsonString,JsonElement.class);
String jsonInString = gson.toJson(json);
System.out.println(jsonInString);
}
OUTPUT:
{
"action": "Read",
"infos": [
{
"value": 0.0350661,
"key": "first"
}
]
}
Even if I am forming the jsonObject using org.json
, and simple printing it using System.out.println(jsonObject.toString());
on console, m getting the result like this.
{"action":"Read","infos":[{"value":0.0350661,"key":"first"}]}
So here, not sure how you have formed your jsonObject.
专注分享java语言的经验与见解,让所有开发者获益!
评论