json - 在 JSON 中表示 null

标签 json null jackson gson

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

例如,如果我在服务器上的对象有一个名为“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 公约,但有一些异常(exception):

  • 对象优于基元。
  • 空集合优于空集合。
  • 没有值的对象表示为空。
  • 基元返回它们的值。

如果您返回的 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>

最佳答案

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

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

The tl;dr here:

The fragment in the json2 variable is the way the JSON spec indicates null should be represented. But as always, it depends on what you're doing -- sometimes the "right" way to do it doesn't always work for your situation. Use your judgement and make an informed decision.


JSON1 {}

这将返回一个空对象。那里没有数据,它只会告诉您您要查找的任何键(无论是 myCount 还是其他)都是 undefined 类型。


JSON2 {"myCount": null}

在这种情况下,实际上定义了 myCount,尽管它的值为 null。这 not 与“not undefined and not null”相同,如果您正在测试一种或另一种情况,这可能成功,而 JSON1 会失败。

这是根据 the JSON spec 表示 null 的明确方式.


JSON3 {"myCount": 0}

在这种情况下,myCount 为 0。这与 null 不同,也与 false 不同。如果您的条件语句计算 myCount > 0,那么这可能是值得的。此外,如果您基于此处的值运行计算,则 0 可能很有用。但是,如果您尝试测试 null,这实际上根本行不通。


JSON4 {"myString": ""}

在这种情况下,您将得到一个空字符串。同样,与 JSON2 一样,它已定义,但它是空的。您可以测试 if (obj.myString == "") 但您无法测试 nullundefined


JSON5 {"myString": "null"}

这可能会给您带来麻烦,因为您将 string 值设置为 null;在这种情况下,obj.myString == "null" 但是它是 not == null


JSON6 {"myArray": []}

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

关于json - 在 JSON 中表示 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21120999/

相关文章:

mysql - 数据库空字段

java - 将 JSON 反序列化为现有对象 (Java)

java - Jackson:将 JSON 响应映射到多个 bean

java - 嵌入式Java对象的Jackson反序列化

json - 手动使用 Jira Rest API 与以编程方式使用 Jira Rest API 有区别吗?

json - grails初始化bean加载顺序

javascript - 迭代 django 模板中的 json 元素

json - 在需要对象交互的站点上使用 XML HTTP 请求

javascript - Asp.net MultiView/使用 Javascript 或 jQuery 检查 ActiveViewIndex

当两个参数都可以为 null 时,Equals() 的 C# 简写