是codenameOne JSONObject.toString创建格式错误的JSON与数组周围的引号?

时间:2016-04-13 03:13:42

标签: json codenameone

我正在使用CodenameOne的JSONObject类,当我转换为字符串时,它似乎是在不需要它们的地方添加引号。

如果我正确使用课程,请查看并告诉我。

我从包含ArrayLists的HashMap创建JSON对象,然后将其转换为要发送的字符串:

ArrayList categories;
ArrayList modules;
// add some String values to the Array Lists
HashMap<String,Object> activityData = new HashMap<String,Object>;
activityData.put("categories",categories);
activityData.put("modules",modules);
JSONObject json = new JSONObject(activityData);
//more unrelated code and then...
String jsonString = json.toString();

问题是hashmap中的ArrayList对象被视为带引号的字符串,因此在另一端解析它会将Categories和Modules作为字符串返回,而不是Arrays。

{"categories":"[punches, blocks, kicks]","modules":"[white_to_yellow, yellow_to_orange]"}

所以例如,不是将类别解释为具有3个元素“punches”,“blocks”,“kicks”的数组,而是将其从JSON解码为单个字符串:

categories = "[punches, blocks, kicks]"

所以我错误地使用了这个类,还是这个错误?

1 个答案:

答案 0 :(得分:1)

您使用的是哪个JSONObject类?来自CN1JSON库的那个?

2015年11月有一个类似问题的修复程序。您使用的是旧版本的lib吗?

相关问题