【问题标题】:Codename One - How to Store an Array of JSON data in Persistent Memory and Read it Back代号一 - 如何在持久内存中存储 JSON 数据数组并读回
【发布时间】:2016-11-01 01:30:56
【问题描述】:

在发送 JSON 请求后,我收到了这个 JSON 数据作为响应。我想将此数据存储在持久存储内存或文件中,然后稍后将其读回并作为下拉列表显示在我的屏幕上。如果有人知道如何在 Codename One 中使用它,我将不胜感激。

    {
   result_code=0.0,
   data=   [
      {
         id=1,
         title=Afghanistan
      },
      {
         id=2,
         title=Albania
      },
      {
         id=3,
         title=Algeria
      },
      {
         id=4,
         title=Andorra
      },
      {
         id=5,
         title=Angola
      },
      {
         id=6,
         title=Antigua And Barbuda
      },      

   ],
   message=OK
     }

【问题讨论】:

    标签: json codenameone


    【解决方案1】:

    我只是做了一个小测试(y)

    String x = "{result_code=0.0,data=   [{id=1,  title=Afghanistan   },      {         id=2,         title=Albania      },      {         id=3,         title=Algeria      },      {         id=4,         title=Andorra      },      {         id=5,         title=Angola      },      {         id=6,         title=Antigua And Barbuda      },       ],   message=OK     }";
    Storage.getInstance().writeObject("abcd.txt", x);
    Object rawString = Storage.getInstance().readObject("abcd.txt");
    JSONObject jsonObject = new JSONObject(rawString.toString());
    JSONArray data = (JSONArray) jsonObject.get("data");
    Double resultCode = Double.valueOf(jsonObject.getDouble("result_code"));
    String message = jsonObject.get("message").toString();
    
    // TODO: JSON parsing
    JSONObject firstObjectFromArray = data.getJSONObject(0);
    // TODO: do somestuff in a loop and put into combobox?
    firstObjectFromArray.get("id");
    firstObjectFromArray.get("title");
    

    编辑:你说的是哪个组合框组件?

    【讨论】:

    • 嗨,Kaya,谢谢,但似乎代号一没有或不支持 JSONObject 库。
    • 哦,你可能是对的,但幸运的是,如果你安装了最新的 CN1 插件,那么现在将库导入 CN1 非常容易,例如我有 Eclipse。更新您的插件,然后右键单击您的项目 -> CodenameOne -> CodenameOne 设置。将弹出另一个窗口,选择 Extensions -> now install "Codename One JSON Library",然后再次右键单击您的项目 -> CodenameOne->Refresh cn1libs Libs。
    • 听起来很棒,我是 CN1 的新手,这对我有很大帮助,所以我想循环浏览这些数据并立即在我的屏幕上显示所有数据作为列表序列化的方式,你知道我该怎么做吗?
    • 您可以使用格式化字符串设置标签的文本一次,或者将其设置为 Hardway,使用列表甚至是列表的渲染器,因此我建议观看 Shai 的视频,他们是真的没有那么糟糕。在 Designer 中设计你的东西,(双击 /src 下的 theme.res)然后在你设计你的 UI 之后保存文件。之后,当您在 StateMachine.java 类中时,通过调用“findContainer”或“findButton1”、findJSONLabel(取决于您设置的变量名称)获取 UI 对象并设置它们的值。
    • 嗨,Kaya,我收到此错误 java.lang.ClassCastException: ca.weblite.codename1.json.JSONObject cannot be cast to ca.weblite.codename1.json.JSONArray on this line JSONArray data = (JSONArray ) jsonObject.get("数据");
    猜你喜欢
    • 2021-06-02
    • 2014-03-30
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2013-03-21
    相关资源
    最近更新 更多