简体   繁体   English

使用空JSON对象处理

[英]Handle with null JSON object

I am new to JSON and jQuery. 我是JSON和jQuery的新手。 What I am doing is get the JSON object from the api and display it as a table. 我正在做的是从api获取JSON对象并将其显示为表格。 But some elements in JSON array are null 但是JSON数组中的某些元素为null

For example; 例如; A JSON array looks like this : JSON数组如下所示:

[
  {"key":"first" , "name" :{"last":"Owen"} , "age" :{"number": 18} },
  {"key":"second" , "name" : null , "age" :{"number": 20}  } ,
  {"key":"third" , "name" : {"last":"Jay"} , "age" :null }
]

So to input to the table, I do this: 所以要输入到表中,我这样做:

$.each(jsonarray, function(i,data){
 $("<tr>").append(
   $.('<td>').text(data.key),
   $.('<td>').text(data.name.last),
   $.('<td>').text(data.age.number)).appendTo('.table');
});

so the thing is they will get error: 所以问题是他们会得到错误:

Cannot read property 'last' of null ; 无法读取null的属性“ last”; Cannot read property 'number' of null 无法读取null的属性“ number”

because in the second and third element, name is null and age is null. 因为在第二个和第三个元素中,名称为null,年龄为null。 Therefore, there is no such thing is name.last and age.number to add in. 因此,没有要添加的名称name.lastage.number这样的东西。

What I want is whenever they get something like that, they automatically return "No Information" to add into the table 我想要的是每当他们得到类似的东西时,他们会自动返回“无信息”以添加到表中

However, I still don't know how to do it. 但是,我仍然不知道该怎么做。 I did try to write an function to check. 我确实尝试编写了一个要检查的功能。 For example like this : 例如这样:

    function check1(datum){
    if (datum !== null && datum !== undefined){
        return datum.last;
    }
    else {
        return "No Information"
    }
}
    function check2(datum){
    if (datum !== null && datum !== undefined){
        return datum.number;
    }
    else {
        return "No Information"
    }
}

But it is very tedious because I need to write 2 separate function to check (1 for name and 1 for age). 但这非常繁琐,因为我需要编写2个单独的函数进行检查(名称为1,年龄为1)。 I just wonder is there any elegant way to do one function for all? 我只是想知道有没有一种优雅的方法可以为所有人完成一项功能?

Can this meet your need? 能满足您的需求吗?

var noDataTip = 'No Information';
$.each(jsonarray, function(i,data) {
    $("<tr>").append(
    $('<td>').text(data.key),
    $('<td>').text( (data.name && data.name.last) || noDataTip),
    $('<td>').text( (data.age && data.age.number)) || noDataTip).appendTo('.table');
});

try this. 尝试这个。 :) :)

$.each(jsonarray, function(i,data){
    if(data.key === undefined || data.key === null){
        data.key = 'none';
    }
    if(data.name === undefined || data.name === null){
        data.name = 'none';
    }
    if(data.age === undefined || data.age === null){
        data.age = 'none';
    }
    $("<tr>").append(
        $.('<td>').text(data.key),
        $.('<td>').text(data.name.last),
        $.('<td>').text(data.age.number)
    ).appendTo('.table');
});
    $.each(jsonarray, function(i,data){
        $("<tr>").append(
        $.('<td>').text(data.key),
        $.('<td>').text( data.name && data.name.last ? data.name.last : "No Information"),
        $.('<td>').text( data.age && data.age.number ?
 data.age.number : "No Information").appendTo('.table');
    });

I would write a function that makes a simple check for null: 我将编写一个对null进行简单检查的函数:

function checkForNull(item) {
var data = "No information";
  if(!item === null) {
     data = item;
   }    
   return data;    
}

Then you could use it as follows: 然后,您可以按以下方式使用它:

$.('<td>').text(checkForNull(data.key))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM