Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON into a ListView friendly output

Tags:

json

android

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

like image 248
nobody Avatar asked Dec 30 '09 21:12

nobody


1 Answers

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.

like image 52
CommonsWare Avatar answered Oct 23 '22 20:10

CommonsWare