views:

763

answers:

3

Hi all,

I am building an android app that needs to download and synchronise with an online database, I am sending my query from the app to a php page which returns the relevant rows from a database in JSON format.

can someone please tell me the best way to iterate through a JSON array?

I receive an array of objects:

[{json object},{json object},{json object}]

What is the simplest piece of code I could use to access the JSONObjects in the array?

EDIT: now that I think of it the method I used to iterate the loop was:

for (String row: json){
     id = row.getInt('id');
     name = row.getString('name');
     password = row.getString('password');
}

So I guess I had was somehow able to turn the returned Json into and iterable array. Any Ideas how I could achieve this?

I apologise for my vaguness but I had this working from an example I found on the web and have since been unable to find it.

Thanks

Kevin

A: 

Use the Jackson Java JSON-processor, it's also available for Android.

There are several ways to parse JSON with it:

  • Streaming (like Sax)
  • Tree (like DOM)
  • Object-mapped (like JAXB)

EDIT:

Take org.json classes, there are already in the android API.

PartlyCloudy
Thanks for that but I would prefer to not use any plugs in, I know there is a simple way that I could do it with a simple for each object in the array loop.I had it going before but I cant find it again.
Kevin Bradshaw
Don't reinvent the wheel writing your own JSON parser. Then take this classes and include them into your project: http://www.json.org/java/index.html
PartlyCloudy
Well I only want to do something like:foreach(jsonobject as s){id = s.getInt('id');name = s.getString('name');password = s.getString('password');}Its already there as I have done it successfully before but unfortunatly lost my whole project with a faulty harddrive. I dont want to go adding unnessessary classes when it already exist.. Thanks for your responses though.
Kevin Bradshaw
Yes, you can parse it on your own. As a long as the format does not change it will probably work well. But as soon as you start changing your JSON data, you might run into several issues due to your incomplete JSON parsing. Thus just take this classes and go ahead with it.Otherwise take String.split() and have fun with escaped symbols.
PartlyCloudy
A: 

This article helped me when I got started with JSON and Android:

http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

On Arrays, look for:

JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); 
iPaulPro
+1  A: 

Ive done it two different ways, 1.) make a Map

        HashMap<String, String> applicationSettings = new HashMap<String,String>();
        for(int i = 0; i < settings.length(); i++){
            String value = settings.getJSONObject(i).getString("value");
            String name = settings.getJSONObject(i).getString("name");
            applicationSettings.put(name, value);
        }

2.) make a JSONArray of names

    JSONArray names= json.names();
    JSONArray values = json.toJSONArray(names);
    for(int i = 0 ; i < values.length(); i++){
        if(names.getString(i).equals("description")){
            setDescription(values.getString(i));
        }
        else if(names.getString(i).equals("expiryDate")){
            String dateString = values.getString(i);
            setExpiryDate(stringToDateHelper(dateString)); 
        }
        else if(names.getString(i).equals("id")){
            setId(values.getLong(i));
        }
        else if(names.getString(i).equals("offerCode")){
            setOfferCode(values.getString(i));
        }
        else if(names.getString(i).equals("startDate")){
            String dateString = values.getString(i);
            setStartDate(stringToDateHelper(dateString));
        }
        else if(names.getString(i).equals("title")){
            setTitle(values.getString(i));
        }
    }
schwiz