loop through a json numeric array?
i use $.getJSON and here is my 开发者_运维知识库php file echo:ing back jsonstring
for($i=1; $i<=10; $i++)
{
$nr[] = "nr" . $i;
}
$nr = json_encode($nr);
echo "{'result': 'error', 'count': '$nr'}";
how do i loop all the nr through with jquery html()?
i want to echo it back to webpage like:
nr 1 nr 2 nr 3 nr 4 nr 5 nr 6 nr 7 nr 8 nr 9 nr 10
In jquery, eval the "count" like
array_data=eval(json_data["count"])
php return this
{'result': 'error', 'count': '["nr1","nr2","nr3","nr4","nr5","nr6","nr7","nr8","nr9","nr10"]'}
Once you eval "count"
array_data will be ["nr1","nr2","nr3","nr4","nr5","nr6","nr7","nr8","nr9","nr10"]
After that you can loop array_data
$.getJSON('file.php', function(data){
for(var i=0; i<data.count.length; i++){
alert(i+": "+data.count[i]);
}
});
edit: The problem is that the way you store it, the php array is stored as a string in the json. Try the following instead:
for($i=1; $i<=10; $i++)
{
$nr[] = "nr" . $i;
}
$ json = array('result' => 'error', 'count' => $nr);
echo json_encode($json);
精彩评论