开发者

json null check not working

I am doing the following null check , even thought the object is null its still unabl开发者_StackOverflow中文版e to check that

if(callbackResults.Details[0].Tags!='') // Tags are coming as null from backend..

details is a json object. Tags is again a object array inside details.

the above null check is failing and its going inside the loop

can you tell whats going wrong there.


You can't test for null by comparison with an empty string. Try this:

 if (callbackResults.Details[0].Tags) {
   // not null
 }


That is a check to see if Tags is an empty string. A check for 'not null' in JavaScript is this:

if(callbackResults.Details[0].Tags)

Note that this also checks for a booean 'true' condition, but this is the common way to make sure something is not null in javascript.


Changing the if should do it, as described above. Else, if Tags is an array, you might check if the length is larger than zero:

if(callbackResults.Details[0].Tags.length > 0)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜