简体   繁体   English

无法将 HashSet 解析为 JSONObject 字符串

[英]Can't parse HashSet to JSONObject String

I am trying to Convert HashSet<String> to JSONObject and then Parse the output JSON.我正在尝试将HashSet<String>转换为JSONObject ,然后解析输出 JSON。

Here is what I have tried:这是我尝试过的:

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());

But it's giving me this output and error:但它给了我这个输出和错误:

{"set":[b, a],"list":["a","b"]}
Exception in thread "main" Unexpected character (b) at position 8.

Here, you can see both a and b are strings, in the list both are inside double quotation marks but in the set it's not.在这里,您可以看到 a 和 b 都是字符串,在列表中都在双引号内,但在集合中不是。

I am using org.json.simple v1.1.我正在使用org.json.simple v1.1。

I think this is a problem with org.json.simple library.我认为这是org.json.simple库的问题。

I have used org.json library, and have to do some minor changes in above code to work:我使用了org.json库,并且必须在上面的代码中做一些小的改动才能工作:

JSONObject json = new JSONObject();
json.put("set", new HashSet<>(Arrays.asList("a", "b")));
json.put("list", Arrays.asList("a", "b"));
String jsonString = json.toString();

System.out.println(jsonString);

JSONObject afterParse = new JSONObject(jsonString);
System.out.println(afterParse.toString());

The output of this code is:这段代码的输出是:

{"set":["a","b"],"list":["a","b"]}

when u convert a array of strings to list and then the list to a Set, it is no longer String, but an array of objects hence new HashSet<>(Arrays.asList("a", "b")));当你将一个字符串数组转换为列表,然后将列表转换为一个集合时,它不再是字符串,而是一个对象数组,因此 new HashSet<>(Arrays.asList("a", "b"))); gives "set":[b, a] (without quotes).给出 "set":[b, a] (不带引号)。 And parser.parse(jsonString);和 parser.parse(jsonString); works on Object not array of Objects.适用于对象而不是对象数组。

Try using a list instead of a set as below :尝试使用列表而不是如下集合:

json.put("set", new Arraylist<>(new HashSet<>(Arrays.asList("a", "b"))));

The alternative solution is use com.fasterxml.jackson.databind.ObjectMapper替代解决方案是使用 com.fasterxml.jackson.databind.ObjectMapper

   String str = "str1";
   String str2 = "str2";
   String str4 = "str3";

   Set<String> setObject= new HashSet();
   setObject.add(str);
   setObject.add(str2);
   setObject.add(str4);

   ObjectMapper mapperObj = new ObjectMapper();
   String JSON = mapperObj.writeValueAsString(setObject);

You can try this way, here HashSet converts to JsonArray, and now this array put into the JSONObject using any key refer below cup of code.你可以试试这种方式,这里 HashSet 转换为 JsonArray,现在这个数组使用任何键放入 JSONObject 参考下面的代码杯。

Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
set.add("D");

// Conver to JSONArray
JSONArray jsonArray = new JSONArray(set.toArray());

// JSONArray to JSONObject with Key
JSONObject jsonObject = new JSONObject();
jsonObject.put("List", jsonArray);

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

// Output:
// {"List":["A","B","C","D"]}

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

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