【发布时间】:2011-12-01 13:21:33
【问题描述】:
我的脚本正在从 php 服务器端脚本获取一些数组。
result = jQuery.parseJSON(result);
现在我想检查数组的每个变量。
if (result.a!='') { something.... }
if (result.b!='') { something.... }
....
有没有更好的方法让它像 php 'foreach' 、 'while' 或 smth 一样快速?
更新
这段代码(感谢 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'}的属性
标签: javascript jquery json