1

I have the following code from my php script.

var str = "[{id : 'Gadji Tallo', poste : 'attaquants', taille : '1.85m', poids :'77kg', age :'20'},{id:'Mbaye Diagne', poste :'attaquants', taille : '', poids :'', age :'22'},{id:'Sigamary Diarra', poste :'attaquants', taille : '1.76m', poids :'74kg', age :'29'}]";
var result = JSON.parse(str)

what I want is to parse it as JSON to loop through it. When I run the following code I got this error in my chrome console:

Uncaught SyntaxError: Unexpected token i

Any clue / help is welcom. Thanks

skunk a
  • 235
  • 1
  • 2
  • 10

2 Answers2

2

This isn't JSON but it's almost a literal JavaScript Object and you don't seem to really need JSON.

As you generate this in PHP, simply don't put the quotes and no parsing is needed :

var result = [{id : 'Gadji Tallo', poste : 'attaquants', taille : '1.85m', poids :'77kg', age :'20'},{id:'Mbaye Diagne', poste :'attaquants', taille : '', poids :'', age :'22'},{id:'Sigamary Diarra', poste :'attaquants', taille : '1.76m', poids :'74kg', age :'29'}];
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I've modified my php script to generate a valid JSON such as [{"id":"Gadji Tallo","poste":"attaquants","taille":"1.85m","poids":"77kg","age":"20"}] but still get an error when I want to parse it. – skunk a Nov 04 '13 at 17:39
  • Why do you want to parse it ? If you generate some literal it's directly usable. – Denys Séguret Nov 04 '13 at 17:43
  • what I want to do is to loop through each object. Wehn I execute console.log (msg) when msg equals the string above I got the number of characters when I wan the number of object whithin my array. $`.ajax({....}).done(function( msg ) { console.log(msg ); =>[{"id":"Gadji Tallo","poste":"attaquants","taille":"1.85m","poids":"77kg ","age":"20"},{"id":"Mbaye Diagne","poste":"attaquants","taille":"","poids":"","age":"22"}] console.log (msg.length); => 179` – skunk a Nov 04 '13 at 17:51
  • You use ajax to get the string ? This looks like a totally different question from your initial one... – Denys Séguret Nov 04 '13 at 17:54
  • yes I use ajax to retrieve the string by calling a php script – skunk a Nov 04 '13 at 17:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40519/discussion-between-user1128939-and-dystroy) – skunk a Nov 04 '13 at 17:59
0

You also need to also put quotes around the names, for example:

var str = '[{"id" : "Gadji Tallo", "poste" : "attaquants", "taille" : "1.85m", "poids":"77kg", "age" :"20"}]';

when parsing JSON from a string.

If you are printing JSON from PHP why don't just keep the array in PHP and then print

print "var obj = " . json_encode($array);
Benten
  • 1,014
  • 12
  • 17
  • it's already done `[{"id":"Gadji Tallo","poste":"attaquants","taille":"1.85m","poids":"77kg ","age":"20"},{"id":"Mbaye Diagne","poste":"attaquants","taille":"","poids":"","age":"22"}]` – skunk a Nov 04 '13 at 17:56
  • same result with json_encode when I run a lenght on the result – skunk a Nov 04 '13 at 18:04
  • Why did you "run a length on the result"? It's a property, not a method. – Benten Nov 04 '13 at 20:03
  • because I want to loop through the result (coming from an ajax call) which is a string representing an array of object. So I run length property to check if my result is well evaluated (by giving number of object in the array and not the char length. What I want is to retrieve the value of each property in the object. – skunk a Nov 05 '13 at 09:04