android – 将JSON解析为ListView友好输出

前端之家收集整理的这篇文章主要介绍了android – 将JSON解析为ListView友好输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有这个 JSON,然后我的活动检索到一个字符串:
{"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","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","id":"22243"},{
            ...
         }]
    }
}

它也可以被访问here,虽然因为它是一个很长的字符串,我已经修改了上面的内容显示所需的内容.

基本上,我希望能够访问“items_last_week”中的项目并创建它们的列表 – 最初我的计划是在左侧显示缩略图”,旁边有“项目”,但是从今天的SDK看起来太难或不可能实现这一点,所以我很高兴只从列表中的’items_last_week’获取’item’数据.

来自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(并且希望合理地说“正确”),否则做出正确的做法将是乏味的.在Web上,性能/带宽问题是用户的问题 – 在移动设备中,它们是您的问题.

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

新的JSONObject(数据)是一行代码.现在,从前面提到的URL获取JSON(我认为你正在做的)将是几行代码.解析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独有的 – JSON解析器在许多其他基于Java的项目中使用.

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会出现类似的现象.

你的选择是:

>创建一个包装已解析JSON的自定义ListAdapter.
>将解析后的JSON转换为MatrixCursor(想想2D数据数组)并使用SimpleCursorAdapter.
>将解析后的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相关文章