Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON array javascript

I need to store 3 pet names in string format, parse them into array and later read one by one

Example

pets = '{{"name":"jack"},{"name":"john"},{name:"joe"}}';  var arr = JSON.parse(pets);  alert(arr[0].name); 

But it doesn't work.

Also I would need to add entry to array (probably with push) but I am having problems too.

Someone has idea how to do it?

like image 479
Joe Doe Avatar asked Feb 23 '12 18:02

Joe Doe


1 Answers

Your JSON is malformed. Try this:

var pets = '{"pets":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}'; var arr = JSON.parse(pets); alert(arr.pets[0].name); 
like image 139
Bradley Mountford Avatar answered Sep 29 '22 21:09

Bradley Mountford