Reading a json encoded array in javascript -
this php function returns json encoded object javascript through ajax. create variable json object stringify.
var event_id = json.stringify(rdata.event_id);
when print variable looks this.
[{"0":"e20111129215359"},{"0":"e20120301133826"},{"0":"e20120301184354"},{"0":"e20120301193226"},{"0":"e20120301193505"},{"0":"e20120303182807"},{"0":"e20120303205512"},{"0":"e20120303211019"},{"0":"e20120306182514"},{"0":"e20120307122044"}]
how access each element of event_id?
don't stringify
it. it's valid javascript object, access directly with:
rdata.event_id[0]["0"]; // e20111129215359 // or read them in loop (var i=0; i<rdata.event_id.length; i++) { console.log(rdata.event_id[i]["0"]; }
the value rdata.event_id
array []
containing bunch of object literals {}
each having 1 property "0"
. since property number instead of string, need use ["0"]
syntax access it, rather normal object dot operator.
Comments
Post a Comment