我试图转换HashSet<String>为JSONObject然后解析输出JSON.
这是我尝试过的:
JSONObject json = new JSONObject();
json.put("set", new HashSet<>(Arrays.asList("a", "b")));
json.put("list", Arrays.asList("a", "b"));
String jsonString = json.toJSONString();
System.out.println(jsonString);
JSONParser parser = new JSONParser();
JSONObject afterParse = (JSONObject) parser.parse(jsonString);
System.out.println(afterParse.toJSONString());
Run Code Online (Sandbox Code Playgroud)
但它给了我这个输出和错误:
{"set":[b, a],"list":["a","b"]}
Exception in thread "main" Unexpected character (b) at position 8.
Run Code Online (Sandbox Code Playgroud)
在这里,你可以看到a和b都是字符串,在列表中都是双引号内但在集合中它不是.
我正在使用org.json.simplev1.1.