JS json.parse "null" values
Elfo Bianco.3786:
Hello guys,
I have already used the GW2 api with C#.NET with good results, but now I’m trying to use them client side with Javascript. I get json data and I parse it with the following line code var jParsed = JSON.parse(jData);
With a full json list of objects everything goes good, but when there are null objects/values with .NET I can make a For loop to check if an object is null or not, while in Javascript even “null” objects seem to be “not null”.
I’m testing JS with API:2/account/bank so there are some bank’s slots that could be null (empty slot in game).
json data block example (API:2/account/bank):
[{"id":67777,“count”: 151,“binding”:"Account"},
{"id":44640,“count”: 52,“binding”:"Account"},
{"id":44640,“count” :250,“binding”:"Account"},
{"id":45001,“count”: 23,“binding”:"Account"},
null,
{"id":68109,“count”: 107,“binding”:"Account"},
{"id":43766,“count”: 250,“binding”:"Account"}]
Now I need to read json data of the items in the bank, and before it I check if a bank’s slot is empty or not. In Javascript I check if an object is null or not.
javascript check code:
for (var i = 0; i < jParsed.length; i++) {
if (jParsed[i] !== null) {
//listIds.push(jBank[it].id);
}
}
The code above seems to work but the “if” condition returns True even for the fifth object that should be null. I have noticed it because from the console of Chrome I get “Uncaught TypeError: Cannot read property ‘id’ of null”.
Does someone have any tips to skip null objects?
I’m using javascript for the first time and I didn’t understand why I couldn’t check with simply “if (jParsed[i] != null)” instead of “if (jParsed[i] !== null)”, but anyway I hope in some help
Anhim.3156:
I don’t know if this is “proper” or “pretty”, but couldn’t you just use
if(jParsed[i]){
…
}
Elfo Bianco.3786:
I don’t know if this is “proper” or “pretty”, but couldn’t you just use
if(jParsed[i]){
…
}
I have tested it too, but it returns True even for “null” objects. Really I don’t understand why.
Actually I have done a new JS code block:
for (var i = 0; i < jParsed.length; i++) {
var temp = JSON.stringify(jParsed[i]);
if (jParsed[i] != “null”) {
//listIds.push(jBank[it].id);
}
}
The last solution works but I hope that could be an easier/faster way to check an object directly without stringify it again..
Anhim.3156:
The only thing that comes to mind then is that your initial “null” is not the null object. Is it perhaps already a string, or some other form?
Elfo Bianco.3786:
The only thing that comes to mind then is that your initial “null” is not the null object. Is it perhaps already a string, or some other form?
No it was an object. Now (after a PC crash) all works with “if (jParsed[i] != null)”, maybe something went wrong with visual studio debug -_-
potatofarms.9241:
I’m assuming it must have been something wrong with the visual studio debug. Your code should (and does) work.
Here’s a JS fiddle: https://jsfiddle.net/eq8wb3t0/2/
Elfo Bianco.3786:
I’m assuming it must have been something wrong with the visual studio debug. Your code should (and does) work.
Here’s a JS fiddle: https://jsfiddle.net/eq8wb3t0/2/
Yes it was visual studio. It was hard to imagine that visual studio debug could be bugged
Thank you for your help guys