在JSON中返回空值的首选方法是什么?对原语有不同的偏好吗?

例如,如果我在服务器上的对象有一个名为“myCount”的整数,没有值,最正确的JSON值将是:

{}

or

{
    "myCount": null
}

or

{
    "myCount": 0
}

同样的问题字符串-如果我有一个空字符串“myString”在服务器上,是最好的JSON:

{}

or

{
    "myString": null
}

or

{
    "myString": ""
}

或者(上帝帮助我)

{
    "myString": "null"
}

我喜欢在JSON中将集合表示为空集合http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html的约定

一个空数组将被表示:

{
    "myArray": []
}

编辑总结

“个人偏好”的论点似乎是现实的,但目光短浅,作为一个社区,我们将消费越来越多的不同的服务/资源。JSON结构的约定将有助于规范上述服务的使用和重用。至于建立标准,我建议采用杰克逊的大部分约定,只有少数例外:

对象优先于原语。 空集合优先于空集合。 没有值的对象表示为null。 原语返回它们的值。

如果返回的JSON对象大部分为空值,则可能需要将其重构为多个服务。

{
    "value1": null,
    "value2": null,
    "text1": null,
    "text2": "hello",
    "intValue": 0, //use primitive only if you are absolutely sure the answer is 0
    "myList": [],
    "myEmptyList": null, //NOT BEST PRACTICE - return [] instead
    "boolean1": null, //use primitive only if you are absolutely sure the answer is true/false
    "littleboolean": false
}

上面的JSON是从下面的Java类生成的。

package jackson;    
import java.util.ArrayList;
import java.util.List;    
import com.fasterxml.jackson.databind.ObjectMapper;
    
public class JacksonApp {    
    public static class Data {    
        public Integer value1;    
        public Integer value2;
        public String text1;
        public String text2 = "hello";
        public int intValue;
        public List<Object> myList = new ArrayList<Object>();
        public List<Object> myEmptyList;
        public Boolean boolean1;
        public boolean littleboolean;   
   }
    
   public static void main(String[] args) throws Exception {
       ObjectMapper mapper = new ObjectMapper();
       System.out.println(mapper.writeValueAsString(new Data()));
   }
}

Maven的依赖:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.3.0</version>
</dependency>

我会为变量的数据类型选择“默认”(null为字符串/对象,0为数字),但确实检查将消费对象期望的代码。不要忘记,有时空/默认和。“不存在”。

检查空对象模式-有时传递一些特殊的对象而不是null更好(例如,对于数组,[]数组而不是null,对于字符串,“”)。

这是个人的、情境性的选择。重要的是要记住,空字符串和数字0在概念上与null是不同的。

在计数的情况下,您可能总是想要一些有效的数字(除非计数未知或未定义),但在字符串的情况下,谁知道呢?在应用程序中,空字符串可能意味着什么。也可能不是。这由你来决定。

我将使用null来表示该特定键没有值。例如,用null表示“您的家庭中连接到互联网的设备数量”是未知的。

另一方面,如果特定的键不适用,则使用{}。例如,对于向没有任何汽车的人询问“拥有活跃互联网连接的汽车数量”的问题,您不应该显示计数,即使为null。

我会避免默认值,除非默认值有意义。虽然你可以决定使用null来表示没有值,但绝对不要使用“null”来这样做。

让我们来评估一下每一个的解析:

http://jsfiddle.net/brandonscript/Y2dGv/

var json1 = '{}';
var json2 = '{"myCount": null}';
var json3 = '{"myCount": 0}';
var json4 = '{"myString": ""}';
var json5 = '{"myString": "null"}';
var json6 = '{"myArray": []}';

console.log(JSON.parse(json1)); // {}
console.log(JSON.parse(json2)); // {myCount: null}
console.log(JSON.parse(json3)); // {myCount: 0}
console.log(JSON.parse(json4)); // {myString: ""}
console.log(JSON.parse(json5)); // {myString: "null"}
console.log(JSON.parse(json6)); // {myArray: []}

这里的tl;dr: json2变量中的片段是JSON规范指示应该表示null的方式。但一如既往,这取决于你在做什么——有时“正确”的方法并不总是适用于你的情况。运用你的判断力,做出明智的决定。


JSON1 {}

这将返回一个空对象。这里没有数据,它只会告诉您您正在寻找的任何键(无论是myCount还是其他键)是未定义的类型。


JSON2 {"myCount": null}

在本例中,实际上定义了myCount,尽管它的值为null。这与“非未定义且非空”不同,如果您正在测试一个条件或另一个条件,这可能会成功,而JSON1将失败。

这是JSON规范中表示null的最终方式。


孙3(“我数”:0)

在本例中,myCount为0。它不等于null,也不等于false。如果条件语句计算myCount > 0,那么这可能是值得的。此外,如果基于这里的值运行计算,0可能有用。然而,如果你试图测试为空,这实际上是行不通的。


JSON4 {“myString”: “”}

在本例中,您将得到一个空字符串。同样,与JSON2一样,它是有定义的,但它是空的。你可以测试if (obj。myString == ""),但你不能测试null或undefined。


JSON5 {"myString": "null"}

这可能会给你带来麻烦,因为你将字符串值设置为null;在本例中,是obj。myString == "null",但它不是== null。


JSON6 {“myArray”: []}

这将告诉你你的数组myArray存在,但它是空的。如果您试图对myArray执行计数或求值,这是非常有用的。例如,假设您想要计算用户发布的照片数量—您可以使用myArray。长度,它将返回0:已定义,但没有照片张贴。

Null不是零。它本身不是一个值:它是一个变量域之外的值,表示缺失或未知数据。

在JSON中只有一种表示null的方法。根据规范(RFC 4627和json.org):

2.1. 值 JSON值必须是对象、数组、数字、字符串或其中之一 以下三个字面名称: False null true

只有一种方法来表示null;这是null。

console.log(null === null);   // true
console.log(null === true);   // false
console.log(null === false);  // false
console.log(null === 'null'); // false
console.log(null === "null"); // false
console.log(null === "");     // false
console.log(null === []);     // false
console.log(null === 0);      // false

也就是说;如果任何客户端使用您的JSON表示使用===操作符;这对他们来说可能是个问题。

没有任何价值

如果你想传达你有一个myCount属性没有值的对象:

{ "myCount": null }

没有属性/缺少属性

如果你想传达你有一个没有属性的对象:

{}

客户端代码将尝试访问myCount并获得undefined;它不在那里。

空集合

如果你想传达你有一个带有myCount属性的对象,它是一个空列表:

{ "myCount": [] }

According to the JSON spec, the outermost container does not have to be a dictionary (or 'object') as implied in most of the comments above. It can also be a list or a bare value (i.e. string, number, boolean or null). If you want to represent a null value in JSON, the entire JSON string (excluding the quotes containing the JSON string) is simply null. No braces, no brackets, no quotes. You could specify a dictionary containing a key with a null value ({"key1":null}), or a list with a null value ([null]), but these are not null values themselves - they are proper dictionaries and lists. Similarly, an empty dictionary ({}) or an empty list ([]) are perfectly fine, but aren't null either.

在Python中:

>>> print json.loads('{"key1":null}')
{u'key1': None}
>>> print json.loads('[null]')
[None]
>>> print json.loads('[]')
[]
>>> print json.loads('{}')
{}
>>> print json.loads('null')
None

'null'最适合实际使用

FWIW,以PHP为例,PHP将空集解释为PHP创建的条目…

// This loop will iterate one item with the value 'the car'
$empty_json = '["the car"]';
$some_json_array = json_decode($empty_json);

foreach ($some_json_array as $i) {
  echo "PHP sees one item";
}

输出:PHP看到汽车

// This loop will iterate one item, but with no values
$empty_json = '[""]';
$some_json_array = json_decode($empty_json);

foreach ($some_json_array as $i) {
  echo "PHP sees: $i";
}

输出:PHP看到

// This loop will iterate nothing because PHP's `json_decode()` function knew it was `null`
$empty_json = 'null';
$some_json_array = json_decode($empty_json);

foreach ($some_json_array as $i) {
  echo "PHP sees one item";
}

输出:(什么都没有,foreach不会循环)