3

1.I am having json object like this how can i get the access to all values in android
2.before anyone down voting that i want to say i have searched stackoverflow for similar examples but those examples contains JSONObject and then JSONArray
3.Here in my case everything is JSONObject so i'm little bit confused

{
"1":
{"sno":"10","latitude":"31.249441437085455","longitude":"75.70003598928452"},

"2":
{"sno":"11","latitude":"31.249398442090207","longitude":"75.70003397762775"}
}

This is how i made it to JSONObject
1.Below code returned me output like that which is all JSONObject
2.Is there any way to make "1" and "2" as JSONArray ?

$select_rows = "select sno,latitude,longitude from activities where username='$username'";
if ($result = mysql_query($select_rows)) {

$num_of_fields = mysql_num_fields($result);
$num_of_rows = mysql_num_rows($result);

 $i = 0;

    $j = 1;

    while ($retrieved = mysql_fetch_array($result)) {

        for ($i = 0; $i < $num_of_fields; $i++) {

            $field_name = mysql_field_name($result, $i);

            $object[$j][$field_name] = $retrieved[$field_name];
        }
        $j++;
    }
    echo json_encode($object);
}
2
  • 1
    The only way to parse all the values in a JSONObject is knowing the tags for all items. You would be better off using an array in your json rather than naming them 1, 2, etc. Commented Apr 27, 2014 at 1:36
  • @dcharms i have elaborated how i would got that output.. can you please suggest me how to make JSONArray if you have any idea.. otherwise thanks for your efforts... Commented Apr 27, 2014 at 1:47

3 Answers 3

7

Hope this may help you

JSONObject jsonObject=new JSONObject(result);

            jsonObject=jsonObject.getJSONObject("1");

            Double lat=jsonObject.getDouble("latitude");




            Toast.makeText(getBaseContext(),""+ lat, Toast.LENGTH_LONG).show();
Sign up to request clarification or add additional context in comments.

Comments

5

Better create a valid JSONArray to make things easier. When you create the JSON string , don't append the index. I believe your are reading the data from sqlite and generating the JSON string.

Also change the outer { (curly brace) to [ (square bracket) to represent as Array element. So your json string will be like this

[
{
    "sno": "10",
    "latitude": "31.249441437085455",
    "longitude": "75.70003598928452"
},
{
    "sno": "11",
    "latitude": "31.249398442090207",
    "longitude": "75.70003397762775"
}
]

Then , you can simply get its as JSONArray

JSONArray jsonArray = new JSONArray(jsonString);

And then read each element in array item

 for(int index = 0;index < jsonArray.length(); index++) {
    JSONObject jsonObject = jsonArray.getJSONObject(index);
 }

1 Comment

Hi can you please look at this post.. I am getting no response.. stackoverflow.com/questions/23319920/…
1

To get one of your inner JSONObject elements, you'll need to get it from the outer one using getJSONObject(String key).

Sample code:

JSONObject item1 = myJson.getJSONObject("1");
//item1 now contains {sno:10,latitude:31.2...etc}
//now you can get the individual values out of the resulting JSON Object
//for example, getting the latitude
double lat = item1.getDouble("latitude");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.