Jump to content

Read JSonArray Elements

WolvesWorld's Photo
Posted Aug 17 2011 02:15 AM
3307 Views

Dear All;
I wrote a php query which return the following JSON string :
[{"cnt":"0","user":"tamer"},{"cnt":"1","user":"Jose"}]

so In my blackberry application, I wrote the following :

returnString = returnStringBuffer.toString();
	             Dialog.alert(returnString);
	                // examine return string
	             
	             String jsonString =new String("{");
	             jsonString= jsonString + "\""; 
	             jsonString= jsonString +"myArray\" : ";   
	             jsonString= jsonString +returnString;
	             jsonString= jsonString +"}";
	             Dialog.alert(jsonString);
	          // Now the call should succeed
	    
	                JSONObject myjson = new JSONObject(jsonString);
                 
	                JSONArray nameArray = myjson.names();
	               JSONArray valArray = myjson.toJSONArray(nameArray);
	             
	              Dialog.alert( nameArray.getString(0));
	              Dialog.alert( valArray.getString(0));
	             
	              Dialog.alert( nameArray.getString(0));
	                if (returnString.indexOf("1") != -1)
	                {
	                    ret = true;
	                }

So the returnString is the string from the httpconection it works fine uptill now.

Now I want to assign the returnString to JsonObject ... then I want to access for example like that :
valArray["cnt"][0];
valArray["user"][0];

how it should be done ???
regards...

Tags:
1 Subscribe


1 Reply

0
  odewahn1's Photo
Posted Aug 17 2011 05:03 PM

Hi. I might be missing something, but just based on the structure of the JSON, I think you need to put the array index first since everything inside the "[...]" is an array, and then the hash name, like this:

valArray[0]["cnt"];
valArray[0]["user"];