比较jQuery中的JSON值 [英] Comparing JSON value in Jquery

查看:121
本文介绍了比较jQuery中的JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查以下JSON中是否存在"T1000"作为批准密钥".如果存在,那么我要显示相应的批准值".

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"
            }
         }
    ]
}

我正在将JSON作为Ajax响应.我试图遍历所有属性,并将其与作为参数传入的值匹配.

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.

这是我的代码,但它只吐出对象Object,对象Object.因此,如果我有5个属性,则此代码会将对象Object吐出5次.

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.

我正在将inputFieldDefaultValue作为参数传递给插件,其值为T1000.因此,o.inputFieldDefaultValue.

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; 
    } 
}); 

如果我这样做

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

会警告相应的值(Amy Dong),但仍会写入对象Object(与JSON响应中的属性一样多).

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

推荐答案

您可以使用以下遍历对象的代码来访问JSON对象的值:

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
  }
}

这篇关于比较jQuery中的JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆