【问题标题】:is codenameOne JSONObject.toString creating malformed JSON with quotes around arrays?codenameOne JSONObject.toString 是在数组周围用引号创建格式错误的 JSON 吗?
【发布时间】: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


    【解决方案1】:

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

    在 2015 年 11 月修复了类似问题。您使用的是旧版本的库吗?

    【讨论】:

    • 我也有同样的问题。使用 CN!图书馆。
    • 请在 CN1JSON 问题跟踪器中使用可重现的测试用例提交问题。此 SO 问题中提供的示例在最新版本的 CN1JSON 中运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多