简体   繁体   English

将 LinkedHashMap 添加到 JSON object 内部的数组时出现问题

[英]Problem With adding an LinkedHashMap to an array inside of JSON object

I have this LinkedHashMap {Label=BEER, ID=179039, URL=https://website.website.com/browse/178974/178973/179039, Children=[]}我有这个 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":[]}我还有一个 JSONObject,里面有一个 ArrayList,如下所示: {"Categories":[]}

I am trying to append this LinkedHashmap to the array inside of the JSONObject, something like this:我正在尝试将这个 LinkedHashmap append 到 JSONObject 内部的数组中,如下所示:

{"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":[]}]}但是,当我这样做时,我打印到控制台,这是 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.如您所见,URL 出现错误,我不确定原因。 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:请注意,当我也只是将 LinkedHashmap 添加到 JSONObject EG 时,就会发生这种情况:

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

I am using import org.json.simple.JSONObject;我正在使用 import org.json.simple.JSONObject;

Does anyone know why it is coming out incorrectly?有谁知道为什么输出不正确?

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;但是如果你想继续使用org.json.simple.JSONObject; you might look up this page: How to add a URL String in a JSON object您可以查看此页面: 如何在 JSON object 中添加 URL 字符串

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

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