0

I have this LinkedHashMap {Label=BEER, ID=179039, URL=https://website.website.com/browse/178974/178973/179039, Children=[]}

I also have a JSONObject with a single ArrayList inside of it like this: {"Categories":[]}

I am trying to append this LinkedHashmap to the array inside of the JSONObject, something like this:

{"Categories":[{Label=BEER, ID=179039, URL=https://website.website.com/browse/178974/178973/179039, Children=[]}]}

However when i do, and i print to the console, this is the output {"Categories":[{"link":"","URL":"https:\/\/Website.Website.com\/browse\/178974\/178973\/179039","Children":[]}]}

As you can see, the URL has come out wrong, and I am unsure as to why. Here is my code:

LinkedHashMap<String,Object> jo=new  LinkedHashMap<String,Object>();
       jo.put("link", "");
       jo.put("URL", "https://Website.Website.com/browse/178974/178973/179039");
       jo.put("Children",new ArrayList<>());
       List<LinkedHashMap<String,Object>> categories = new ArrayList<LinkedHashMap<String,Object>>();
       categories.add(jo);
       JSONObject ja1 = new JSONObject();
       ja1.put("Categories",categories);
       System.out.println(ja1);``` 

Note, this happens when I also simply just add the LinkedHashmap to the JSONObject EG:

{"Categories":{"link":"","URL":"https:\/\/Website.Website.com\/browse\/178974\/178973\/179039","Children":[]}}

I am using import org.json.simple.JSONObject;

Does anyone know why it is coming out incorrectly?

1 Answers1

0

Using your code with :

import org.json.simple.JSONObject;

I also have this string :

{"Categories":[{"link":"","URL":"https:\/\/Website.Website.com\/browse\/178974\/178973\/179039","Children":[]}]}

Using your code with

import org.json.JSONObject; 

I have the following result:

{"Categories":[{"link":"","Children":[],"URL":"https://Website.Website.com/browse/178974/178973/179039"}]}

But if you desired to keep using org.json.simple.JSONObject; you might look up this page : How to add a URL String in a JSON object

Hodvidar
  • 1
  • 2
  • May I ask how you imported that? as when I try to I get The import org.json.JSONObject cannot be resolved – jamiefiska Jul 22 '20 at 15:31
  • I am using IntelliJ IDEA and Maven so it is mainly automated. If you are using Maven you need to the following text add between the tag of your pom.xml file : ` org.json json 20160810 ` – Hodvidar Jul 22 '20 at 15:45
  • If you want to keep your JSON library, use : `System.out.println("org.json.simple.JSONObject toString() : "+ja1); String stringModified = ja1.toString().replace("\\", ""); System.out.println("org.json.simple.JSONObject toString() modified : "+stringModified);` --> org.json.simple.JSONObject toString() : {"Categories":[{"link":"","URL":"https:\\\/\\\/Website.Website.com\\\/browse\\\/178974\\\/178973\\\/179039","Children":[]}]} org.json.simple.JSONObject toString() modified : {"Categories":[{"link":"","URL":"https://Website.Website.com/browse/178974/178973/179039","Children":[]}]} – Hodvidar Jul 22 '20 at 16:01