Sto provando a convertire HashSet<String>
in JSONObject
e quindi analizzare l'output JSON.Impossibile analizzare HashSet in stringa JSONObject
Ecco che cosa ho provato:
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());
Ma mi sta dando questa uscita ed errore:
{"set":[b, a],"list":["a","b"]}
Exception in thread "main" Unexpected character (b) at position 8.
Qui, è possibile vedere sia a che b sono stringhe, nella lista sia sono racchiusi tra virgolette doppie ma nel set non lo è.
Sto usando org.json.simple
v1.1.
Hai provato a fare il vostro HashSet come HashSet? Penso che non risolverà il problema, ma vale la pena di essere testato. –
Ho provato a utilizzare HashSet ma l'output è lo stesso –
Quando ho cambiato il pacchetto in 'org.json.JSONObject', lo stesso codice funziona. Non sono sicuro che possa essere un bug in org.json.simple. – Ravikumar