简体   繁体   English

JSON简单中的双引号

[英]double quote in json simple

I want this outcome: 我想要这个结果:

{"link":[{"url":"http://en.wikipedia.org/wiki/JScript", "label":"wikipedia"}]}

I tried this: 我尝试了这个:

JSONObject ob1 = new JSONObject();
ob1.put("link","[{\"url\":\"http://en.wikipedia.org/wiki/JavaScript\", label:\"wikipedia\"}]");

The output of ob1.toJSONString() is: ob1.toJSONString()的输出是:

{"link":"[{\"url\":\"http:\/\/en.wikipedia.org\/wiki\/JavaScript\", label:\"wikipedia\"}]"}

What am I doing wrongly? 我做错了什么? I am using json-simple-1.1.1 我正在使用json-simple-1.1.1

You should put a JSONArray into the JSONObject and in the Array again a JSONObject with the keys/values "url"/"http://en.wikipedia.org/wiki/JScript" and "label"/"wikipedia" 您应该将JSONArray放入JSONObject,然后在Array中再次放入具有键/值"url"/"http://en.wikipedia.org/wiki/JScript""label"/"wikipedia"的JSONObject。

JSONObject ob1 = new JSONObject();
JSONArray ar1 = new JSONArray();
ob1.put ("link", ar1);

JSONObject ob2 = new JSONObject();
ar1.add(ob2);

ob2.put("url", "http://en.wikipedia.org/wiki/JScript");
ob2.put("label", "wikipedia");

In case you have the value of ob1 link object already as JSON string then you can first interpret this into a JSONArray using 如果您已经将ob1链接对象的值作为JSON字符串,则可以使用以下方法首先将其解释为JSONArray:

JSONArray ar1 = (JSONArray)JSONValue.parse(yourJSONStr);

NOTE: Your intended result is not a JSON string, because for that all "/" must be escaped "\\/" 注意:您的预期结果不是JSON字符串,因为为此必须将所有"/"转义为"\\/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM