anson920520 anson920520 - 5 months ago 7
JSON Question

Checker null value of JSON by jquery

for example i acquire some json data like below

errorCode: null,
message: "Success",
result: {
keywordData: null,
},
totalRecord: 1,
checkAccess: true,
token: xxxxx


As you see the keywordData is null.

And i am having a if statment in my JS like below

if(Object.keys(result.keywordData.length !== 0)){

//do something

}


but it's not working, how may i check null value?

full JSON:

{
errorCode: null,
message: "Success",
result: {
keywordData: null,
keywords: null,
lastSyncDate: 1465168445000,
profileId: 129,
overallTrend: null
},
totalRecord: 1,
checkAccess: true,
token: "123"
}

Answer

You've not provided the full structure of the object, so I'm assuming this is what it looks like:

var object = {
    errorCode: null,
    message: "Success",
    result: {
        keywordData: null,
    },
    totalRecord: 1,
    checkAccess: true,
    token: xxxxx
};

If this is indeed the structure of the object, then you can simply do this:

if (object.result.keywordData) {
    // do something
}
Comments