2009-12-06 209 views
0

我使用$ .getJSON,这里是我的PHP文件的回声:掀背jsonstring通过json数组数组循环?

for($i=1; $i<=10; $i++) 
{ 
    $nr[] = "nr" . $i; 
} 

$nr = json_encode($nr); 

echo "{'result': 'error', 'count': '$nr'}"; 

怎么做所有的NR通过与jQuery文档i循环()?

我想回声回网页等:

NR 1 NR 2 NR 3 NR 4 NR 5 NR 6 NR 7 NR 8 NR 9 NR 10

回答

1

在jquery中,评估“count”like

array_data=eval(json_data["count"]) 

php返回此

{'result': 'error', 'count': '["nr1","nr2","nr3","nr4","nr5","nr6","nr7","nr8","nr9","nr10"]'} 

一旦你EVAL “计数”

array_data将["nr1","nr2","nr3","nr4","nr5","nr6","nr7","nr8","nr9","nr10"]

之后,你可以循环array_data

+0

如果count变量保存不同的字符串值,该怎么办? – 2009-12-06 09:03:34

+0

好的,同样的,我也更新了我的答案。 – YOU 2009-12-06 09:07:12

0
$.getJSON('file.php', function(data){ 
    for(var i=0; i<data.count.length; i++){ 
    alert(i+": "+data.count[i]); 
    } 
}); 

编辑:问题是你存储的方式它,php数组作为字符串存储在json中。请尝试改为:

for($i=1; $i<=10; $i++) 
{ 
    $nr[] = "nr" . $i; 
} 
$ json = array('result' => 'error', 'count' => $nr); 
echo json_encode($json);