【问题标题】:How to check and then only retrive from JSON如何检查然后仅从 JSON 中检索
【发布时间】:2020-06-30 13:37:18
【问题描述】:

我收到以下 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" 缺失,它就会中断并且我变得不确定

是否可以先检查是否存在,然后再检索?

https://jsfiddle.net/o2gxgz9r/9078/

关于答案,我将我的 json 修改为

{
                "user_details": [
                {
                     "Name": "Mark",
                        "Age": "35",
                        "Gender": "Male",
                        "Country": "US"
                        }
              ]
        }

但是 hasOwnProperty 不起作用?

请看这个小提琴

https://jsfiddle.net/o2gxgz9r/9085/

【问题讨论】:

  • 您需要重构该 JSON。 user_details 中的数组是不必要的,并且无法预测给定字段名称的数组索引(如果缺少“名称”,则所有其余部分都向下移动。)只需将其设为普通的旧对象即可。
  • 重新更新:这样更好,但仍然不需要数组!只需使用普通对象,例如 "user_details": { "Name": "Mark", "Age": "35" }

标签: jquery


【解决方案1】:

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

理想的方式应该是如下所示的对象映射或向量:

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

【讨论】:

  • 我更改了我的 JSON 结构,但为什么 hasOwnProperty 不起作用?jsfiddle.net/o2gxgz9r/9084
  • 您使用的是数组,而应该使用对象。如果多个用户的数据即将到来,则应使用对象数组。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    让它更通用一些尝试类似这样的方法,它将遍历每个项目的 user_details 数组设置属性。

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

    这有一个额外的好处是结果集中的任何新属性都将被自动处理。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 2021-09-21
        相关资源
        最近更新 更多