[ACCEPTED]-null / empty json how to check for it and not output?-json

Accepted answer
Score: 19

Since JSON is simply a data format, there 10 really is no way to know which of your data 9 members will be null unless you explicitly 8 check them. You can always refactor your 7 code to make it more compact and easier 6 to read, but you will have to check each 5 item explicitly if you do not know beforehand 4 which will be null and which will contain 3 data.

While I don't know what your code is 2 supposed to do, here is an example of how 1 you might refactor it to make it more compact:

var data = { Name: "John Doe", Age: 25, Address: null, CityState: "Denver, CO" };
for (member in data) {
    if (data[member] != null)
        // Do work here
}
Score: 4

I'm not completely sure of what you want 5 to do... you say that you don't want to 4 pass them on to other functions so I assume 3 you want to delete them:

var data = {a:"!",b:"null", c:null, d:0, e:"", hasOwnProperty:"test"};

var y;
for (var x in data) {
    if ( Object.prototype.hasOwnProperty.call(data,x)) {
        y = data[x];
        if (y==="null" || y===null || y==="" || typeof y === "undefined") {
            delete data[x];
        }

    }
}

The check for hasOwnProperty 2 is to make sure that it isn't some property 1 from the property chain.

Score: 2

Or you could just use the

int data=0;
try{
   data=json.getInt("Data");
}catch(Exception e){
   data=anydefaultdata;
}

0

More Related questions