JSON解析成一个ListView友好的输出 [英] Parse JSON into a ListView friendly output

查看:106
本文介绍了JSON解析成一个ListView友好的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有此JSON,然后我的活动检索到的字符串:

  {流行:
        {authors_last_month:[
         {
            URL:http://activeden.net/user/OXYLUS
            项目:OXYLUS
            销售:1148,
            形象:http://s3.envato.com/files/15599.jpg
         },
         {
            URL:http://activeden.net/user/digitalscience
            项目:digitalscience
            销售:681,
            形象:http://s3.envato.com/files/232005.jpg
         }
         {
            ...
         }
        ]
        items_last_week:[
         {
            成本:4.00,
            缩略图:http://s3.envato.com/files/227943.jpg
            URL:http://activeden.net/item/christmas-decoration-balls/75682
            销售:43,
            项目:圣诞装饰球,
            评级:3,
            ID:75682
         },
         {
            成本:30.00,
            缩略图:http://s3.envato.com/files/226221.jpg
            URL:http://activeden.net/item/xml-flip-book-as3/63869
            销售:27,
            项目:XML翻转书/ AS3
            评级:5,
            ID:63869
         },
         {
            ...
         }],
        items_last_three_months:[
         {
            成本:5.00,
            缩略图:http://s3.envato.com/files/195638.jpg
            URL:http://activeden.net/item/image-logo-shiner-effect/55085
            销售:641,
            项目:形象标志赛诺效应,
            评级:5,
            ID:55085
         },
         {
            成本:15.00,
            缩略图:http://s3.envato.com/files/180749.png
            URL:http://activeden.net/item/banner-rotator-with-auto-delay-time/22243
            销售:533,
            项目:BANNER ROTATOR带自动延迟时间,
            评级:5,
            ID:22243},
         {
            ...
         }]
    }
}
 

可以访问这里为好,尽管它,因为它是一个相当长的字符串,我已经修剪上面下来,以显示所需要的。

基本上,我希望能够从items_last_week访问项目和创建它们的名单 - 本来我的计划是对留下的旁边有项目的缩略图,但是从打与周围的SDK今天它来实现这似乎太困难或不可能的,所以我会很乐意与只拥有在列表中的items_last_week'的'项目'的数据。

从PHP来到我挣扎使用任何的JSON库,可用于Java的,因为它似乎要高于一线code更多,我需要反序列化(我认为这是正确的字)的JSON,他们似乎都需要某种形式的额外的类,除了JSONArray / JSONObject的脚本,我不喜欢这样的事实items_last_week嵌套(同样,我认为这是JSON的术语),并采取了可怕的很长一段时间,以在Android模拟器中运行。

所以,实际上,我需要一个(preferably简单)的方式向items_last_week数据传递到ListView。我知道我需要一个自定义适配器,我可以大概让我的头左右,但我不明白,不管多少日子我刚刚花了试图弄明白,如何访问一个JSON字符串的某些部分..

解决方案
  

本来我的计划是让   在左边的缩略图   它旁边的'项目',但打   周围的今天SDK出现   太困难或不可能实现   这种

这是远远不行的,但是这将是乏味的得到的权利,除非您使用一些已经包装了该模式的你(和希望是合理的权利)。在网络上,性能/带宽问题是用户的问题 - 在移动,他们是你的问题

  

,因为它似乎比一个更   的code线,我需要   反序列化(我认为这是正确的   字)的JSON

新的JSONObject(数据)是一行code。现在的读取的JSON的,这是我presume你从上述网址做,将是几行code。在JSON的无论是解析,也不是它从互联网抓取是唯一到Android - 所有这一切看起来一样在桌面的Java应用程序,或者一个Java servlet,或任何

  

距JSONArray /的JSONObject   剧本我不喜欢的   事实上items_last_week是嵌套

我还没有一个问题的JSON解析与像你的文件展品结构。此外,这是几乎唯一的机器人 - 的JSON解析器被用在许多其它基于Java的项目

  

和需要一个可怕的长时间运行的   Android模拟器

仿真器的速度依赖于你的开发机器的速度。对我来说,模拟器通常比实际手机硬件慢......和我的桌面是一个四核。请记住,仿真器pretending是您的PC上运行的ARM芯片,将ARM运codeS到86运codeS的飞行,所以它不会要快,不会利用多个内核非常出色。

  

所以,实际上,我需要一个(preferably   简单)的方式来传递   items_last_week数据到ListView。

没有什么真正建设成为Android的采取任意JSON结构,使用任意数据,并直接倒入一个的ListView 。这是不是唯一的JSON - XML将出现类似的现象。

您的选择是:

  1. 创建一个自定义的 ListAdapter 封装了解析后的JSON。
  2. 转换所解析的JSON成 MatrixCursor (认为数据的二维数组),并使用 SimpleCursorAdapter
  3. 转换所解析的JSON到的ArrayList<字符串> 和使用 ArrayAdapter

在短期内,#3选项可能是最简单的。

  

我知道我需要一个定制   适配器,我也许可以让我的   头左右,但我不明白,   无论我怎么了一天多所   刚刚花了试图弄明白,   如何访问一个JSON的某些部分   字符串..

这问题太模糊了太多的援助方式。你可能会考虑开辟一个单独的问题,标记为Java和JSON,在那里你进入其中,您有与json.org解析器问题的细节。

So I have this JSON, which then my activity retrieves to a string:

    {"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"},
         {
            ...
         }]
    }
}

It can be accessed here as well, although it because it's quite a long string, I've trimmed the above down to display what is needed.

Basically, I want to be able to access the items from "items_last_week" and create a list of them - 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, so I would be more than happy with just having the 'item' data from 'items_last_week' in the list.

Coming from php I'm struggling to use any of the JSON libraries which are available to Java, 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, and they all appear to require some form of additional class, apart from the JSONArray/JSONObject script I have which doesn't like the fact that items_last_week is nested (again, I think that's the JSON terminology) and takes an awful long time to run on the Android emulator.

So, in effect, I need a (preferably simple) way to pass the items_last_week data to a ListView. 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..

解决方案

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

It is far from impossible, but it will be tedious to get right, unless you use something that already wraps up that pattern for you (and that hopefully is reasonably "right"). On the Web, performance/bandwidth issues were the user's problem -- in mobile, they're your problem.

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) is one line of code. Now, fetching the JSON, which I presume you are doing from the aforementioned URL, will be several lines of code. Neither the parsing of the JSON nor the fetching of it off the Internet is unique to Android -- all of that would look the same on a desktop Java app, or a Java servlet, or whatever.

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

I have not had a problem parsing JSON with structures like your file exhibits. Moreover, this is hardly unique to Android -- the JSON parser is used in many other Java-based projects.

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

The speed of the emulator is tied to the speed of your development machine. For me, the emulator is usually slower than actual phone hardware...and my desktop is a quad-core. Bear in mind that the emulator is pretending to be an ARM chipset running on your PC, converting ARM opcodes into x86 opcodes on the fly, so it's not going to be fast and won't leverage multiple cores very well.

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

There is nothing really built into Android to take an arbitrary JSON structure, with arbitrary data, and directly pour it into a ListView. This is not unique to JSON -- XML would exhibit similar phenomenon.

Your choices are:

  1. Create a custom ListAdapter that wraps the parsed JSON.
  2. Convert the parsed JSON into a MatrixCursor (think 2D array of data) and use a SimpleCursorAdapter.
  3. Convert the parsed JSON into an ArrayList<String> and use an ArrayAdapter.

For the short term, option #3 is probably the simplest.

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..

And that question is too vague for much in the way of assistance. You might consider opening up a separate question, tagged for Java and JSON, where you get into the details of where you are having problems with the json.org parser.

这篇关于JSON解析成一个ListView友好的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆