Parse JSON into friendly ListView output

So, I have this JSON, which then extracts my activity into 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 , although this is because it is a rather long line, I cropped above to display what is needed.

Basically, I want to have access to the items from "items_last_week" and create a list of them - initially my plan was to have a "thumbnail" on the left with a "item" next to it, but today, playing with the SDK seems too complicated or impossible to achieve this, so I would be more than happy just having the "item" data from the "items_last_week" in the list.

Based on php, I try my best to use any of the JSON libraries that are available for Java, as this is apparently much larger than the line of code I will need to deserialize (I think the right word) is JSON, and they all need some form of additional class besides the JSONArray / JSONObject script, I don’t like the fact that items_last_week is nested (again, I think the JSON terminology) and awfully long time to run on the Android emulator.

So, essentially, I need a (preferably a simple) way to pass items_last_week data to a ListView. I understand that I will need a custom adapter that I can probably work around, but I can’t understand, no matter how much time I spent trying to figure out how to access some parts of the JSON string.

+6
json android
source share
2 answers

initially my plan was to “thumbnail” to the left with the “element” next to it, but from playing around with the SDK today it appears too difficult or impossible to achieve this

This is far from impossible, but it will be tiring if you do not use what is already completing this template for you (and, hopefully, reasonably "right"). On the Internet, performance / bandwidth problems were the user's problem - on mobile devices, this is your problem.

since it seems a lot bigger than the line of code that I will need to deserialize (I believe the right word) is JSON

new JSONObject(data) - one line of code. Now choosing a JSON, which I assume you are making from the above URL, will consist of a few lines of code. Neither JSON parsing nor fetching from the Internet is unique to Android — it will all look the same on a Java desktop application or Java servlet or something else.

besides JSONArray / JSONObject script I have that I don't like the fact that items_last_week is nested

I had no problem parsing JSON with structures like your files. Moreover, this is hardly unique to Android - the JSON parser is used in many other Java projects.

and takes a long time to run the Android emulator

The speed of the emulator is tied to the speed of your development machine. For me, an emulator is usually slower than the actual hardware of the phone ... and my desktop is quad. Keep in mind that the emulator pretends to be an ARM chipset running on your PC, converting ARM operating codes to x86 operating codes on the fly, so it will not be fast and will not use multiple cores efficiently.

So, essentially, I need a (preferably simple) way to pass items_last_week to a ListView.

In Android, there is really nothing to take an arbitrary JSON structure with arbitrary data and directly inject it into a ListView . This is not unique to JSON - XML ​​will exhibit a similar phenomenon.

Your options:

  • Create a custom ListAdapter that completes the parsed JSON.
  • Convert the parsed JSON to a MatrixCursor (think of a two-dimensional data array) and use the SimpleCursorAdapter .
  • Convert the parsed JSON to an ArrayList<String> and use the ArrayAdapter .

In the short term, option number 3 is probably the easiest.

I understand that I will need a custom adapter, which I can probably get a head on, but I can’t understand, no matter how much time I have just spent trying to figure this out, how to access certain parts of the JSON string ..

And this question is too vague on the way to help. Perhaps you should open a separate question, tagged for Java and JSON, where you will find out where you have problems with the json.org parser.

+9
source share

I just answered this question. Check it out for very simple code for a custom adapter to handle JSON.

Jsonadapter

+1
source share

All Articles