简体   繁体   中英

JSON: java.lang.Double null is converted to zero; How should I handle in client?

I have a client that makes AJAX requests. The Java application server creates a list of objects that have java.lang.Double instance fields. Depending on the request, some of these instance fields may be null on all the objects. But when the list of objects is converted (via JSONArray.fromObject) nulls are converted to zero.

So, on the client side, these show up as zero values to the user. How should I handle this so the user does not see these values? I would think it would be best stop the server from sending zero in the first place (perhaps by not sending the field), but I'm not sure how to do this cleanly with JSON. Less favorably, I could figure out someway on the client to know what was requested in the handler and prevent showing data unless the client requested. In that case, is there a way to get the HTTP request param data from the XMLHttpRequest object?

Many thanks in advance for your help.

Use a different JSON parser (Crockford's is one possibility). That nulls as 0 stuff seems like a bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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