tags:

views:

78

answers:

3

guys,

I've converted my php result array to json. Now I want to know is this in the correct json format. My code echo json_encode($row); I have to use it with jquery auto complete plugin...

{"0":"1","id":"1","1":"Albania","country":"Albania"} 
{"0":"2","id":"2","1":"Algeria","country":"Algeria`"}
{"0":"3","id":"3","1":"Angola","country":"Angola"}
{"0":"4","id":"4","1":"Anguilla","country":"Anguilla"}
{"0":"5","id":"5","1":"Antigua","country":"Antigua"}
{"0":"6","id":"6","1":"Argentina","country":"Argentina"}
{"0":"7","id":"7","1":"Armenia","country":"Armenia"}
{"0":"8","id":"8","1":"Aruba","country":"Aruba"}
{"0":"9","id":"9","1":"Australia","country":"Australia"}
{"0":"10","id":"10","1":"Austria","country":"Austria"}
{"0":"11","id":"11","1":"Azerbaijan","country":"Azerbaijan"}
0":"26","id":"26","1":"Bulgaria","country":"Bulgaria"}
{"0":"27","id":"27","1":"Burkina Faso","country":"Burkina Faso"}
+1  A: 

Nope. The objects need to be in a top container, e.g. [{...},{...},...].

Ignacio Vazquez-Abrams
@Ignacio should I Eval() it...
chandru_cp
It's WRONG. `eval()` won't help.
Ignacio Vazquez-Abrams
echo '[' . json_encode($row) . ']';However, there seems to be other issues with your code, so you might want to check that out first. (For example, there seems to be an encode error within the json call as it's not rendering correctly on the site, an example of that can be found within your code bit here: "country":"Azerbaijan"}0":"26","See how all of the colors are off after that part? That's a problem.
Mark Tomlin
@Mark Tomlin: All I see now is blonde, brunette, redhead. xD
Alix Axel
@Alix Axel: Is that an Armageddon reference? Steve Buscemi's character saying then during the psyche eval?
Mark Tomlin
@Mark Tomlin: It's the MATRIX! =)
Alix Axel
+2  A: 

Taken from the PHP Manual.

A true json object will be wrapped in { }, while a json array will be wrapped, like Ignacio says in [ ].

Additionally, you can use the options in json_encode to force the formatting. Check your plugin to see if it specifies which format they want it in.

Edit

After looking more closely at your object, what you probably want to do is put each row into an array and then json_encode that array.

$json = array();
while($row = mysql_fetch_array($query)){
   $json[] = $row;
}

echo json_encode($json);

Right now, it looks like you're outputting a different json object for each row.

munch
+6  A: 

Each object need to wrap in [] and need comma between them as other said,

And the object which contains Bulgaria is broken like this

0":"26","id":"26","1":"Bulgaria","country":"Bulgaria"}

it should be

{"0":"26","id":"26","1":"Bulgaria","country":"Bulgaria"}

Correct one would be like

[{"0":"1","id":"1","1":"Albania","country":"Albania"},
{"0":"2","id":"2","1":"Algeria","country":"Algeria`"},
{"0":"3","id":"3","1":"Angola","country":"Angola"},
{"0":"4","id":"4","1":"Anguilla","country":"Anguilla"},
{"0":"5","id":"5","1":"Antigua","country":"Antigua"},
{"0":"6","id":"6","1":"Argentina","country":"Argentina"},
{"0":"7","id":"7","1":"Armenia","country":"Armenia"},
{"0":"8","id":"8","1":"Aruba","country":"Aruba"},
{"0":"9","id":"9","1":"Australia","country":"Australia"},
{"0":"10","id":"10","1":"Austria","country":"Austria"},
{"0":"11","id":"11","1":"Azerbaijan","country":"Azerbaijan"},
{"0":"26","id":"26","1":"Bulgaria","country":"Bulgaria"},
{"0":"27","id":"27","1":"Burkina Faso","country":"Burkina Faso"}]
S.Mark