android - 将 JSON 解析为 ListView 友好的输出

标签 android json

所以我有这个 JSON,然后我的 Activity 将其检索到一个字符串:

    {"popular":
        {"authors_last_month": [
         {
            "url":"http://activeden.net/user/OXYLUS",
            "item":"OXYLUS",
            "sales":"1148",
            "image":"http://s3.envato.com/files/15599.jpg"
         },
         {
            "url":"http://activeden.net/user/digitalscience",
            "item":"digitalscience",
            "sales":"681",
            "image":"http://s3.envato.com/files/232005.jpg"
         }
         {
            ...
         }
        ],
        "items_last_week": [
         {
            "cost":"4.00",
            "thumbnail":"http://s3.envato.com/files/227943.jpg",
            "url":"http://activeden.net/item/christmas-decoration-balls/75682",
            "sales":"43",
            "item":"Christmas Decoration Balls",
            "rating":"3",
            "id":"75682"
         },
         {
            "cost":"30.00",
            "thumbnail":"http://s3.envato.com/files/226221.jpg",
            "url":"http://activeden.net/item/xml-flip-book-as3/63869",
            "sales":"27",
            "item":"XML Flip Book / AS3",
            "rating":"5",
            "id":"63869"
         },
         {
            ...
         }],
        "items_last_three_months": [
         {
            "cost":"5.00",
            "thumbnail":"http://s3.envato.com/files/195638.jpg",
            "url":"http://activeden.net/item/image-logo-shiner-effect/55085",
            "sales":"641",
            "item":"image logo shiner effect",
            "rating":"5",
            "id":"55085"
         },
         {
            "cost":"15.00",
            "thumbnail":"http://s3.envato.com/files/180749.png",
            "url":"http://activeden.net/item/banner-rotator-with-auto-delay-time/22243",
            "sales":"533",
            "item":"BANNER ROTATOR with Auto Delay Time",
            "rating":"5",
            "id":"22243"},
         {
            ...
         }]
    }
}

可以访问here同样,虽然它是一个很长的字符串,但我已经将上面的内容缩减以显示需要的内容。

基本上,我希望能够访问“items_last_week”中的项目并创建它们的列表 - 最初我的计划是在左侧放置“缩略图”,旁边是“项目”,但是从播放今天的 SDK 似乎很难或不可能实现这一点,所以我会非常高兴列表中只有来自“items_last_week”的“项目”数据。

来自 php 我正在努力使用 Java 可用的任何 JSON 库,因为它似乎不仅仅是一行我需要反序列化的代码(我认为这是正确的词) JSON,它们似乎都需要某种形式的附加类,除了我拥有的 JSONArray/JSONObject 脚本之外,它不喜欢 items_last_week 是嵌套的(同样,我认为这是 JSON 术语)并且需要很长时间在 Android 模拟器上运行。

因此,实际上,我需要一种(最好是简单的)方法将 items_last_week 数据传递给 ListView。我知道我需要一个自定义适配器,我可能会想到它,但我无法理解,无论我花了多少时间试图弄明白,如何访问 JSON 字符串的某些部分..

最佳答案

originally my plan was to have the 'thumbnail' on the left with the 'item' next to it, but from playing around with the SDK today it appears too difficult or impossible to achieve this

这远非不可能,但除非您使用 something that already wraps up that pattern,否则正确操作会很乏味。对你来说(希望这是合理的“正确”)。在网络上,性能/带宽问题是用户的问题——在移动设备上,它们是您的问题。

as it appears to be much more than a line of code which I will need to deserialize (I think that's the right word) the JSON

new JSONObject(data)是一行代码。现在,获取 JSON(我假设您是从上述 URL 执行的)将需要几行代码。 JSON 的解析和从互联网上获取它都不是 Android 独有的——所有这些在桌面 Java 应用程序、Java servlet 或其他任何东西上看起来都是一样的。

apart from the JSONArray/JSONObject script I have which doesn't like the fact that items_last_week is nested

我在使用您的文件展示的结构解析 JSON 时没有遇到问题。此外,这并不是 Android 独有的——许多其他基于 Java 的项目都使用了 JSON 解析器。

and takes an awful long time to run on the Android emulator

模拟器的速度与开发机器的速度相关。对我来说,模拟器通常比实际的手机硬件慢……而且我的台式机是四核的。请记住,模拟器假装是在您的 PC 上运行的 ARM 芯片组,即时将 ARM 操作码转换为 x86 操作码,因此它不会很快,也不会很好地利用多核。

So, in effect, I need a (preferably simple) way to pass the items_last_week data to a ListView.

Android 中并没有真正内置任何东西来采用任意 JSON 结构和任意数据,并将其直接倒入 ListView 中。 .这不是 JSON 独有的——XML 也会表现出类似的现象。

您的选择是:

  1. 创建自定义 ListAdapter包装已解析的 JSON。
  2. 将解析后的 JSON 转换为 MatrixCursor (考虑二维数据数组)并使用 SimpleCursorAdapter .
  3. 将解析后的 JSON 转换为 ArrayList<String>并使用 ArrayAdapter .

就短期而言,选项 #3 可能是最简单的。

I understand I will need a custom adapter which I can probably get my head around but I cannot understand, no matter how much of the day I've just spent trying to figure it out, how to access certain parts of a JSON string..

而且这个问题对于提供帮助的方式来说太模糊了。您可能会考虑打开一个单独的问题,标记为 Java 和 JSON,您可以在其中详细了解 json.org 解析器出现问题的位置。

关于android - 将 JSON 解析为 ListView 友好的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1982700/

相关文章:

java - SQLITE 文件大小小?

javascript - 将项目添加到数组 Angular

javascript - API Puller 中的代码块,我不知道发生了什么

java - JSON 对象未正确检测

android - Android环境解析的500KB json文件

mysql 从数组中提取json元素

java - google volley 单例真的线程安全吗?

android - com.jayway.maven.plugins.android.generation2 : Plugin execution not covered by lifecycle configuration?

android - Gradle 项目刷新失败 - 协议(protocol)系列不可用

android - LayoutLib 太新了。更新你的工具?