我的脚本从php服务器端脚本获取了一些数组。针对JSON数组的foreach,语法
result = jQuery.parseJSON(result);
现在我想检查数组中的每个变量。
if (result.a!='') { something.... }
if (result.b!='') { something.... }
....
有没有什么更好的方法使它像PHP的'foreach',''while'或smth?
UPDATE
此代码(感谢hvgotcodes)给我的变量值的数组里面,但我怎么能得到的变量也的名字?
for(var k in result) {
alert(result[k]);
}
更新2
这是方如何PHP的作品
$json = json_encode(array("a" => "test", "b" => "test", "c" => "test", "d" => "test"));
如果我没有记错的话,你可以用它来访问每个JSON对象的属性的名称,就像您的属性。例如'JSONObject.name'来获取JSONObject中包含'{'name':'foo'}'' –