Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse json encoded array and fetch values

How to parse these JSon array am getting from json_encode(arr), the output is:

[{"id":"44","data":"[[10],[27],[13]]","_types":"ff"},  {"id":"44","data":"[[140],[327],[213]]","_types":"44f"}]

I need to iterate this using java script and fetch each values. Am getting an error, Unexpected token.

like image 372
AmDGrtst Avatar asked Apr 23 '26 08:04

AmDGrtst


2 Answers

You are getting a string, you simply need to do JSON.parse

var str = '[{"id":"44","data":"[[10],[27],[13]]","_types":"ff"},  {"id":"44","data":"[[140],[327],[213]]","_types":"44f"}]';

var obj = JSON.parse( str );

alert( obj[0].id );
like image 102
gurvinder372 Avatar answered Apr 25 '26 21:04

gurvinder372


You are getting array of java script object,i think you don't need to parse this.Just iterate this and fetch the result like below.

var q = [{"id":"44","data":"[[10],[27],[13]]","_types":"ff"},  {"id":"44","data":"[[140],[327],[213]]","_types":"44f"}];

 q.forEach(function(i){console.log(i.id)});
like image 41
dReAmEr Avatar answered Apr 25 '26 23:04

dReAmEr