处理 JSON null 和空数组及对象

2026-07-10 21:35:18 6665

Null 值

JSON 具有特殊值 null,可以对任何数据类型设置该值,包括数组、对象、数字和布尔类型。

34 { Schema types

35 "id":null, (integer)

36 "firstName": null, (string)

37 "address": null, (Address complex type with maxOccurs = 1)

38 "homeAddresses":null (Address complex type with maxOccurs > 1)

39 "phoneNumbers": null (string with maxOccurs > 1)

40 }

考虑上一个示例,其中具有 null 值的 JSON 数据被解析为数据对象,以下情况成立:

id - 如果在该模式中将该属性定义为可空,那么它将设置为 null。如果该属性未定义为可空,那么它将抛出异常。

firstName - 在该属性上设置 null 值。

address - 如果在该模式中将该属性定义为可空,那么它将设置为 null。如果该属性未定义为可空,那么它将抛出异常。

homeAddresses - 模式不允许该属性可空,因此未设置该属性。

phoneNumbers - 必须在该模式中将该属性定义为可空,否则它将抛出异常。

当序列化为 JSON 时,如果数据对象中的属性值为 null,那么它将序列化为 JSON null。