Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following array returned to my JAVA Android application from PHP:

Array ( [0] => Array ( [referral_fullname] => Name 1 [referral_balance] => 500 ) [1] => Array ( [referral_fullname] => Name 2 [referral_balance] => 500 ) );

In Java they above array looks like this:

{"0":{"referral_fullname":"Name 1","referral_balance":"500"},"1":{"referral_fullname":"Name 2","referral_balance":"500"}};

For a simple JSONObject I'm using:

JSONTokener tokener = new JSONTokener(result.toString());
JSONObject finalResult = new JSONObject(tokener);

referral_fullname = finalResult.getString("referral_fullname");

but for an array of objects I don't know!

share|improve this question
2  
try to improve your accept rate, then you get answers. –  Sajmon Jun 19 '12 at 12:38
    
Does this work for you? stackoverflow.com/questions/5650171/… –  Slartibartfast Jun 19 '12 at 12:38
    
accept your old questions to increase your accept rate ; –  Houcine Jun 19 '12 at 12:58
1  
0% is way bad, please work on it by accepting answers –  Fahim Parkar Jun 19 '12 at 13:16
add comment

4 Answers

String str = <your json>;
    JSONObject jObject  = new JSONObject(s);


    Map<String,String> map = new HashMap<String,String>();
    Iterator iter = jObject.keys();
    while(iter.hasNext()){
        String key = (String)iter.next();
        String value = jObject .getString(key);
        map.put(key,value);
    }
share|improve this answer
add comment

Your Json Syntax is wrong , JSONArray should be like this :

["0":{"referral_fullname":"Name 1","referral_balance":"500"},"1":{"referral_fullname":"Name 2","referral_balance":"500"}];

and to parse a JsonArray that contains some JSONObject , try this :

//parse the result
            JSONObject jsonResult = null;
            JSONArray arrayResult = null;
            ArrayList<YourObject> listObjects = null;
            try {
                arrayResult = new JSONArray(result);
                if(arrayResult != null) {
                    listObjects = new ArrayList<YourObject>();
                    int lenght = arrayResult.length();
                    for(int i=0; i< lenght; i++) {
                        JSONObject obj = arrayResult.getJSONObject(i);
                        YourObject object = new YourObject(obj);
                        listObjects.add(object);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

And add a constructor in your Class YourObject to convert your Json to an instance :

public YourObject(JSONObject json) {
    if (!json.isNull("referral_fullname"))
        this.referral_fullname = json.optString("referral_fullname", null);
    if (!json.isNull("referral_balance"))
        this.referral_balance = json.optString("referral_balance", null);
}
share|improve this answer
    
You were right, I checked once more and found that the returned value is not an array of objects but looks like an object of objects. How is that possible? I've tried even to parse it as an array an I get an error saying that a JSONObject cannot be converted into a JSONArray. Should I change something in my PHP code to have it returned as an array of objects or to parse it like it is now? –  Andrei Stalbe Jun 19 '12 at 13:05
    
you should return an array of arrays in your php code , and try to convert it to json with : json_encode(yourArrayOFResult); –  Houcine Jun 19 '12 at 13:08
    
I'm confused about new JSONArray(result). What version of json-lib supports this? I've seen this constructor signature (taking a string argument) documented, but when I try to use json-lib-2.4-jdk15.jar, it says the only constructor for JSONArray takes no arguments (as shown here: json-lib.sourceforge.net/apidocs/jdk15/index.html). –  LarsH Dec 8 '12 at 12:18
    
i'm talking about JSONArray of android json support, it is supported in Android without using any library Json –  Houcine Dec 8 '12 at 18:44
    
OK, thanks. I wonder what version of json-lib Android uses. –  LarsH Dec 8 '12 at 20:28
add comment

You should use

JSONArray finalResult = new JSONArray(tokener);

if you can. You structure is now an object with two fields, 0 and 1, which contains another object. You have to get an array of object in place of this composite object if you want to iterate easily like

JSONObject jso;
for(int i = finalResult.lenght-1; i >=0; i--){
  jso = finalResult.get(i);
  // jso == {"referral_fullname":"Name 1","referral_balance":"500"}

  [whatever]

}
share|improve this answer
add comment

Try this.............

final JSONArray result_array = json.getJSONArray("result"); 

for (int i = 0; i < result.length(); i++) {

JSONObject joObject = result_array.getJSONObject(i);
String jName = joObject.get("referral_fullname").toString();
String jbalance = joObject.get("referral_balance").toString();

                    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.