0

I have a JSON like this return from a php file

[
 {"value":

       {"longitude":"103.8439764",
        "latitude":"1.0345","date":"Tue 04 Jun, 2013",
        "time":"12:27"}},

 {"value":
       {"longitude":"104.8439764",
        "latitude":"1.235","date":"Mon 03 Jun, 2013",
        "time":"12:28"}},
 {"status":
        "his_loc"
 }
]

How can I achieve the data like data.status or else ?

themyth92
  • 1,743
  • 2
  • 17
  • 23

3 Answers3

2

You have to parse the data into an object variable:

var data = JSON.parse( datastring );

After that, you can address certain properties directly (e.g. data.status).

Rob
  • 11,492
  • 14
  • 59
  • 94
  • I dont know but I got this error when I tried to use the JSON.parse Uncaught SyntaxError: Unexpected token o – themyth92 Jun 05 '13 at 08:57
0

Since this is an array, you should access it like

var data = JSON.parse(json_string);
var status = data[2].status
leonhart
  • 1,193
  • 6
  • 12
0

I suggest restructuring the response from the server if possible, since the response is not so well-formatted.

Step 1: Change the response as follows:

{
 "value1":

       {"longitude":"103.8439764",
        "latitude":"1.0345","date":"Tue 04 Jun, 2013",
        "time":"12:27"},

 "value2":
       {"longitude":"104.8439764",
        "latitude":"1.235","date":"Mon 03 Jun, 2013",
        "time":"12:28"},
 "status":
        "his_loc"

}

Step 2: Parse the response to a JSON object.

var json = JSON.parse(responseString);

And now you can access the status as follows:

var status = json.status;

That's it!

Xmindz
  • 1,282
  • 13
  • 34