简体   繁体   English

将Map转换为JSON无法正确解析列表

[英]Converting Map to JSON is not resolving lists correctly

I am having an issue with creating a new JSON object given a Map<String, Object> . 给定Map<String, Object> ,创建新的JSON对象时遇到问题。 For example: 例如:

Map<String, Object> data = new Map<String, Object>();
data.put( "myList", new ArrayList<String>( Arrays.asList("item1", "item2")))
JSONObject json = new JSONObject(data);

What I am getting with json.toString() is 我用json.toString()

"{
  "myList": "[item1, item2]"
 }"

when I should be getting 当我应该得到

"{
  "myList": ["item1", "item2"]
 }"

Notice the quotes around vs. in the array? 注意在阵列周围的报价? Any ideas on how this can be fixed? 关于如何解决的任何想法?

It depends on your JSON implementation - using wslite, the following code 这取决于您的JSON实现-使用wslite,以下代码

Map<String, Object> data = new HashMap<String, Object>();
data.put("myList", new ArrayList<String>(Arrays.asList("item1", "item2")));
JSONObject json = new JSONObject(data);

System.out.println(json);

leads to 导致

{"myList":["item1","item2"]}

at my machine. 在我的机器上。

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

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