在JSON中表示空值

7 浏览
0 Comments

在JSON中表示空值

在JSON中,返回null值的首选方法是什么?原语类型有不同的偏好吗?

例如,如果我的服务器端对象有一个名为“myCount”的整数,但没有值,则该值的最正确的JSON为:

{}

还是

{
    "myCount": null
}

还是

{
    "myCount": 0
}

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

{}

还是

{
    "myString": null
}

还是

{
    "myString": ""
}

还是(天哪,请帮帮我)

{
    "myString": "null"
}

我喜欢使用空集合在JSON中表示集合的惯例(http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html)

一个空数组将表示为:

{
    "myArray": []
}

编辑摘要

“个人喜好”的论点似乎是现实的,但是从长远来看是短视的,因为作为一个社区,我们将消耗越来越多不同的服务/来源。JSON结构的惯例将有助于规范这些服务的消费和重用。至于建立标准,则建议采用大多数Jackson的惯例,但有几个例外:

  • 对象优先于原语。
  • 空集合优先于null。
  • 没有值的对象表示为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 myList = new ArrayList();
        public List 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依赖项:


    com.fasterxml.jackson.core
    jackson-core
    2.3.0

admin 更改状态以发布 2023年5月23日
0
0 Comments

null 不是零。它本身不是一个值:它是超出表明缺失或未知数据的变量域的值。

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

2.1.  Values
A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:
  false null true

enter image description here

0
0 Comments

让我们评估每个解析的方式:

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: []}


总结:

json2变量中的片段是JSON规范表示null的方式。但是一如既往,取决于你要做什么-有时候"正确"的做法并不总是适用于你的情况。要使用你的判断力并做出明智的决策。


JSON1 {}

这将返回一个空对象。没有任何数据,并且只会告诉你你寻找的任何键(无论是myCount还是其他)都是undefined类型。


JSON2 {"myCount": null}

在这种情况下,myCount实际上已经定义,尽管它的值为null。这与"不是undefined且不是null"不同,如果你正在测试一个条件,则此条件可能成功,而JSON1会失败。

这是JSON规范表示null的明确方式。


JSON3 {"myCount": 0}

在这种情况下,myCount是0。这不同于null,也不同于false。如果你的条件语句评估为myCount > 0,则这可能是有用的。此外,如果你要根据这里的值运行计算,则0可能很有用。如果你想测试null,则这实际上根本不起作用。


JSON4 {"myString": ""}

在这种情况下,你得到的是一个空字符串。就像JSON2一样,它被定义了,但是它是空的。你可以测试 if(obj.myString == "") 但是你不能测试 null 或者 undefined


JSON5 {"myString": "null"}

这可能会让你陷入麻烦,因为你把字符串值设置为null;在这种情况下,obj.myString == "null" 但它不是 == null


JSON6 {"myArray": []}

这将告诉你,你的数组myArray存在,但是它是空的。如果你想对 myArray 执行计数或评估,这是有用的。例如,假设你想评估用户发布的照片数量,你可以执行myArray.length,它将返回0:已定义,但没有发布照片。

0