weixin_33737774 2011-08-25 15:10 采纳率: 0%
浏览 81

比较jQuery中的JSON值

I want to check if "T1000" exists as an approval "key" in the following JSON. If it exists, then I want to display the corresponding approval "value".

{"approvals": 
    [
         {"approval":
            {
               "id":"0121920",
               "key":"T100",
               "value":"Ben Tsu"
            }
         },
         {"approval":
            {
               "id":"",
               "key":"T1000",
               "value":"Amy Dong"
            }
         }
    ]
}

I'm getting JSON as an Ajax response. I'm trying to loop through all properties and match it to the value passed in as the parameter.

Here's my code but it only spits out object Object, object Object. So, if I have 5 properties, this code spits out object Object 5 times.

I'm passing inputFieldDefaultValue as a parameter to the plugin with the value being T1000. Hence, o.inputFieldDefaultValue.

$.each(response.approvals, function(index, approvals){ 
    if(approvals.approval.key == o.inputFieldDefaultValue){ 
         approvals.approval.value; 
    } 
}); 

If I do

$.each(response.approvals, function(index, approvals){ 
    if(approvals.approval.key == o.inputFieldDefaultValue){ 
         alert(approvals.approval.value); 
    } 
});

it alerts the corresponding value (Amy Dong) but it still writes object Object (as many times as the properties in the JSON response).

  • 写回答

4条回答 默认 最新

  • weixin_33725270 2011-08-25 15:14
    关注

    You may access the values of the JSON object by using the following code that traverses the objects:

    var approvals = obj.approvals;
    
    for(var i = 0; i < approvals.length; i++)
    {
      if(approvals[i].approval.key = 'T1000')
      {
        // Display approvals[i].approval.value
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接