[英]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.