Jquery 如何检查并仅检索JSON

Jquery 如何检查并仅检索JSON,jquery,Jquery,我收到以下JSON作为响应 { "user_details": [ { "Name": "Mark" }, { "Age": "35" }, { "Gender":

我收到以下JSON作为响应

{
                "user_details": [
                {
                        "Name": "Mark"
                },
                {
                        "Age": "35"
                },
                {
                        "Gender": "Male"
                },
                {
                        "Country": "US"
                }]
}
我正在解析这个JSON,如下所示

 var ajaxresponse = response.user_details;
        if (ajaxresponse.length > 0)
        {
                var Name = ajaxresponse[0].Name;
                var Age = ajaxresponse[1].Age;
                var Gender = ajaxresponse[2].Gender;
                var Country = ajaxresponse[3].Country;
                console.log(Name);
        }
这很好用

我的问题是,如果JSON中的任何一个键丢失了,例如“Name”丢失了,那么它将被破坏,我将无法定义

是否可以检查是否存在,然后重试

关于答案,我将json修改为

{
                "user_details": [
                {
                     "Name": "Mark",
                        "Age": "35",
                        "Gender": "Male",
                        "Country": "US"
                        }
              ]
        }
但是他自己的财产不起作用了

请看这把小提琴


使用javascript的hasOwnProperty函数

if(json_object.hasOwnProperty('name')){
//do struff
}
这里

if (ajaxresponse.length > 0)
    {
        if(ajaxresponse.hasOwnProperty("Name"))
        {
            var Name = ajaxresponse[0].Name;
            var Age = ajaxresponse[1].Age;
            var Gender = ajaxresponse[2].Gender;
            var Country = ajaxresponse[3].Country;
            console.log(Name);
       }
    }

使用javascript的hasOwnProperty函数

if(json_object.hasOwnProperty('name')){
//do struff
}
这里

if (ajaxresponse.length > 0)
    {
        if(ajaxresponse.hasOwnProperty("Name"))
        {
            var Name = ajaxresponse[0].Name;
            var Age = ajaxresponse[1].Age;
            var Gender = ajaxresponse[2].Gender;
            var Country = ajaxresponse[3].Country;
            console.log(Name);
       }
    }

首先,这是一种错误的方式,从任何来源发送数据作为响应

理想的方法应该是对象贴图或向量,如下所示:

user_details: {
   name: "Mark",
   age: 35,
   gender: "male",
   country: "USA"
}
第二,如果您想要获得数据结构的解决方案,那么必须实际遍历每个项的数组,并查看其上是否存在属性

var arr = ajaxResponse;

var name,age,country,age;

arr.forEach(function(item){
    if(item.hasOwnProperty('name')){
        name = item.name
    }
    //similarly for other objects in the array
));

首先,这是一种错误的方式,从任何来源发送数据作为响应

理想的方法应该是对象贴图或向量,如下所示:

user_details: {
   name: "Mark",
   age: 35,
   gender: "male",
   country: "USA"
}
第二,如果您想要获得数据结构的解决方案,那么必须实际遍历每个项的数组,并查看其上是否存在属性

var arr = ajaxResponse;

var name,age,country,age;

arr.forEach(function(item){
    if(item.hasOwnProperty('name')){
        name = item.name
    }
    //similarly for other objects in the array
));

让它更通用一点尝试这样的方法,它将迭代每个项目的
user\u详细信息
数组设置属性

var ajaxresponse = response.user_details;
var user_details = ajaxresponse.reduce(function(details, detail){
    // details is the object we'll populate and will get assigned to user_details
    // detail is the current user detail object - Name/Age/Gender/Country

    // get property name for this "detail"
    var propertyName = Object.getOwnPropertyNames(detail)[0];

    // set the property and value for the current detail object
    details[propertyName] = detail[propertyName];

    // return the updated details object for the next iteration
    return details;
}, {});

console.log(user_details.Name);

这增加了一个额外的好处,即结果集中的任何新属性都将自动处理。

让它更通用一点,尝试类似的方法,它将迭代每个项目的
用户\u详细信息
数组设置属性

var ajaxresponse = response.user_details;
var user_details = ajaxresponse.reduce(function(details, detail){
    // details is the object we'll populate and will get assigned to user_details
    // detail is the current user detail object - Name/Age/Gender/Country

    // get property name for this "detail"
    var propertyName = Object.getOwnPropertyNames(detail)[0];

    // set the property and value for the current detail object
    details[propertyName] = detail[propertyName];

    // return the updated details object for the next iteration
    return details;
}, {});

console.log(user_details.Name);

这增加了一个额外的好处,即结果集中的任何新属性都将被自动处理。

下面的代码可以检查JSON数组中存在的属性,还可以从数组中获取属性的值

array.forEach(item=>{
          if(item.hasOwnProperty(propertyname)){
              if(item[propertyname]){
                resultArray.push(item[propertyname])
              }
           }
          
        })

下面的代码可以检查JSON数组中存在的属性,也可以从数组中获取属性的值

array.forEach(item=>{
          if(item.hasOwnProperty(propertyname)){
              if(item[propertyname]){
                resultArray.push(item[propertyname])
              }
           }
          
        })

也许你需要重构JSON。
user\u details
中的数组是不必要的,并且使得无法预测给定字段名的数组索引(如果缺少“name”,则所有其余的都向下移动)。只需将其设置为普通的旧对象即可。重新更新:这样更好,但数组仍然是不必要的!只需使用一个普通对象,如
“user_details”:{“Name”:“Mark”,“Age”:“35”}
也许您需要重构该JSON。
user\u details
中的数组是不必要的,并且使得无法预测给定字段名的数组索引(如果缺少“name”,则所有其余的都向下移动)。只需将其设置为普通的旧对象即可。重新更新:这样更好,但数组仍然是不必要的!只需使用普通对象,如
“user_details”:{“Name”:“Mark”,“Age”:“35”}
我更改了JSON结构,但为什么hasOwnProperty不起作用?您使用的是数组,而应该使用对象。如果多个用户的数据即将到来,则应使用对象数组。我更改了JSON结构,但为什么hasOwnProperty不起作用?您使用的是数组,而应该使用对象。如果多个用户的数据即将到来,则应使用对象数组。