最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON with empty numeral value? (not 0) - Stack Overflow

programmeradmin2浏览0评论

Given a JSON such:

[
{ "id":"A", "status": 1, "rank":1, "score":  },
{ "id":"B", "status": 1, "rank":1, "score":  }
]

My script fails due to the empty score.

Given a JS such :

if (json[i].score) { do something } else { calculate it } 

I want to keep the field empty, and not use 0. I may use "score": "", but this will imply that it's a string (empty at start), while I want score to be a numeral (empty at start). So the number I push in it stay a number, and not a string.

How to state an empty/undefined numeral ?


Note: I intuitively think it's why I sometime meet undefined.


EDIT: the question was edited to clarify the context, the need to check the existence of obj.score, where 0 would be misleading.

Given a JSON such:

[
{ "id":"A", "status": 1, "rank":1, "score":  },
{ "id":"B", "status": 1, "rank":1, "score":  }
]

My script fails due to the empty score.

Given a JS such :

if (json[i].score) { do something } else { calculate it } 

I want to keep the field empty, and not use 0. I may use "score": "", but this will imply that it's a string (empty at start), while I want score to be a numeral (empty at start). So the number I push in it stay a number, and not a string.

How to state an empty/undefined numeral ?


Note: I intuitively think it's why I sometime meet undefined.


EDIT: the question was edited to clarify the context, the need to check the existence of obj.score, where 0 would be misleading.

Share Improve this question edited Jul 12, 2013 at 1:18 Hugolpz asked Jul 12, 2013 at 0:42 HugolpzHugolpz 18.3k28 gold badges105 silver badges191 bronze badges 8
  • 8 How about null? – elclanrs Commented Jul 12, 2013 at 0:44
  • Don't know. I'am the newbee – Hugolpz Commented Jul 12, 2013 at 0:45
  • you could try something like parseInt(''), but get you somewhere but I think if it's a numeric value it's going to want something in there, even if it's just a 0 (although I'll admit I've never had to look into that for anything). – Phillip Gooch Commented Jul 12, 2013 at 0:45
  • 2 Test like: if (obj.score != null) // checks null & undefined – elclanrs Commented Jul 12, 2013 at 1:00
  • 1 @elclanrs: Douglas Crowford would scream at you for using "!=" (jslint.) :) – zizozu Commented Jul 12, 2013 at 1:31
 |  Show 3 more ments

3 Answers 3

Reset to default 5

TL;DR: Use 0 (zero), if everyone starts with zero score. Use null, if you specifically want to say that someone's score is not set yet. Do not define the property, if it should not be placed in specific case (like object that should not even have "score" property).

Omitting the value in JSON object

Well, in JSON you are not allowed to just omit the value. It must be set to something like number (integer or float), object, list, boolean, null or string. To read more about syntax, try this resource: http://json/. This is the diagram taken from that site, showing you the syntax of object representations in JSON:

Most popular approaches: null, 0 (zero), undefined

The usual approach is to set null. In other cases it can be better to use 0 (zero), if applicable (eg. the field means "sum").

Another approach is just to not set the property. After deserialization you are then able to perform tests checking the existence of specific property like that:

  • JavaScript:

    if (typeof my_object.score !== 'undefined'){
        // "score" key exists in the object
    }
    
  • Python:

    if 'score' in my_object:
        pass  # "score" key exists in the object
    
  • PHP:

    if (array_key_exists('score', $my_object)) {
        // "score" key exists in the object
    }
    

Less consistent approaches: false, ""

Some people also accept the value to be false in such case, but it is rather inconsistent. Similarly when it es to empty string (""). However, both cases should be properly supported in most programming languages during deserialization.

Why not just start score at 0? Everyone will start with a score of 0 in just about anything involving score.

[
  { "id":"A", "status": 1, "rank":1, "score": 0 },
  { "id":"B", "status": 1, "rank":1, "score": 0 }
]

By definition, a numeral value is a value type, which cannot be null. Only reference types (like strings, arrays, etc.) should be initialized to null.

For the semantic of your situation, I would suggest you to use a boolean to know if weither or not there is a score to be read.

[
    { "id":"A", "status": 1, "rank":1, "empty":true  },
    { "id":"B", "status": 1, "rank":1, "empty":false, "score":100}
]

Then,

if (!foo.empty) {
    var score = foo.score;
}

While a null could be tested as well, it is a wrong representation of a number.

发布评论

评论列表(0)

  1. 暂无评论