【发布时间】:2016-08-03 22:17:22
【问题描述】:
我正在使用 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":"[punches, blocks, kicks]","modules":"[white_to_yellow, yellow_to_orange]"}
因此,例如,类别不是被解释为具有 3 个元素“punches”、“blocks”、“kicks”的数组,而是从 JSON 中解码为单个字符串:
categories = "[punches, blocks, kicks]"
是我错误地使用了这个类,还是这是一个错误?
【问题讨论】:
标签: json codenameone