javascript - Get specific info out of json -
this question has answer here:
- php values json encode 2 answers
how can "coords" data out of json object in jquery? , each object own coords?
example of returned json php shown in console:
[ { "id":"7", "name":"example", "address":"example adress", "coords":"96.0,17.0" }, { same here etc. } ]
edit: problem can't access json object in way recommended. if type object[0]
"[" if type object[1]
"{" other characters in sequence '"', "i", "d", etc.
i console log in sucessfull ajax call so:
.done(function(data) { console.log(data[2]); });
and php returns data so:
echo json_encode($mydata);
your data coming string, need 1 of 2 things json format:
- in
.ajax
call, set optiondatatype: 'json'
alongsideurl
,data
etc parameters ($.ajax({ url: 'x.php', datatype: 'json' ... }).done(function...
) - manually parse json in
.done()
function (.done(function(data) { data = $.parsejson(data); ... });
)
this convert json format, original answer below applied.
since json array @ outermost level ([ ]
), can access first-level items array notation (eg. [0]
). inside that, have object ({ }
) need dot-notation there (eg. [0].coords
).
assuming variable called myjson
can @ first coords myjson[0].coords
, second set myjson[1].coords
, etc. can loop on myjson
coords
in loop.
Comments
Post a Comment