Can't parse HashSet to JSONObject String

can definition
can synonym
can verb
can noun meaning
can bus
can airport
can network
can band

I am trying to Convert HashSet<String> to JSONObject and then Parse the output 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.

I am using org.json.simple v1.1.

I think this is a problem with org.json.simple library.

I have used org.json library, and have to do some minor changes in above code to work:

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"]}

Can, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can � Drink can � Oil can � Steel and tin cans � Trash can� Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

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"))); gives "set":[b, a] (without quotes). And 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"))));

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� HANGZHOU, China, Aug. 04, 2020 (GLOBE NEWSWIRE) -- Canaan Inc. (NASDAQ: CAN) ("Canaan" or the "Company"), a leading high-performance computing solutions provider, today announced that the terms of

The alternative solution is use 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);

The Official CAN / Spoon Records Website, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). Containers. Aluminum can; Drink can; Oil can; Steel and tin cans; Trash can; Petrol can; Music. Can (band), West Germany, 1968 Can, 1979; Can (South Korean band) Other. Cans are headphones that enclose the ears

can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. "Can" is one of the most commonly used modal verbs in English. It can be used to express ability or opportunity, to request or offer permission, and to show possibility or impossibility.

Can, can noun [C] (CONTAINER). a metal container, esp. a closed, cylindrical container in which food and drink are packaged:. We would like to show you a description here but the site won’t allow us.

CAN, Word forms � 1. countable noun A can is a metal container in which something such as food, drink, or paint is put. � 2. verb [usually passive] When food or drink is� Bring communication and connectivity in your embedded design to the next level with Microchip's Controller Area Network (CAN) bus solutions technology. Originally created for automotive applications, the CAN protocol is a high-speed, reliable communication protocol for applications requiring robust communication at bit rates reaching 8 Mbps.

Comments
  • Did you try to make your HashSet as a HashSet<String> ? I think it will not solve the problem, but it worth being tested.
  • I have tried using HashSet<String> but the output is same
  • When I changed the package to org.json.JSONObject, same code works. Not sure it may be a bug in org.json.simple.
  • 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"))); gives "set":[b, a] (without quotes). And parser.parse(jsonString); works on Object not array of Objects.
  • I have changed this json.put("set", new HashSet<>(Arrays.asList("a", "b"))); to HashSet<String> set = new HashSet<>(); set.add("a"); set.add("b"); json.put("set", set); but not working
  • i know that if i convert set to list it will work. but i wanted to convert HashSet to JSON directly
  • @Ashraful Islam , converting Hashset which is a collection to json will give error with json simple parse method. Using a object like List will do the deal .