开发者

Is this a correct JSON Data Structure?

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开发者_Go百科 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"}


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"}]


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.


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


Great resource for JSON Validation: http://www.jsonlint.com/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜