json阵列与null

发布于 2025-01-27 22:09:24 字数 379 浏览 4 评论 0原文

以下是解析以验证PUT请求主体的JSON:

{
    "requestKey": "whatever",
    "shoppingList": []
}

作为测试的一部分,我们看到了另一种情况,这也是有效的JSON:

{
    "requestKey": "whatever",
    "shoppingList": [null]
}

[]表示空的购物列表。 [null]表示什么?

[null][]有何不同?对于购物清单

Below is the JSON that is parsed to validate PUT request body:

{
    "requestKey": "whatever",
    "shoppingList": []
}

As part of testing, we are seeing another scenario coming, which is also a valid JSON:

{
    "requestKey": "whatever",
    "shoppingList": [null]
}

[] signifies empty shopping list. What does [null] signify?

How [null] different from []? for shoppingList

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏辞 2025-02-03 22:09:24

null在这种情况下,如 rfc 7159 购物清单是一个包含一个原始空值的数组。

至少从语义上讲,它应该屈服(源示例在Java中):

shoppingList.get(0) -> null 

而空数组应屈服:

shoppingList.get(0) -> IndexOutOfBoundsException

这正是您在当前版本的Google Chrome中看到的行为:

let myObj = {
    "requestKey": "whatever",
    "shoppingList": [null]
};
undefined
myObj
{requestKey: 'whatever', shoppingList: Array(1)}requestKey: "whatever"shoppingList: Array(1)0: nulllength: 1[[Prototype]]: Array(0)[[Prototype]]: Object
let myObj = {
    "requestKey": "whatever",
    "shoppingList": []
};
undefined
myObj
{requestKey: 'whatever', shoppingList: Array(0)}requestKey: "whatever"shoppingList: [][[Prototype]]: Object

null in this case and as referenced in RFC 7159 means that the shoppingList is an array containing one primitive null value.

At least semantically it should yield in (Source example is in Java):

shoppingList.get(0) -> null 

Whereas an empty array should yield in:

shoppingList.get(0) -> IndexOutOfBoundsException

Which is exactly the behaviour you see in the current version of Google Chrome:

let myObj = {
    "requestKey": "whatever",
    "shoppingList": [null]
};
undefined
myObj
{requestKey: 'whatever', shoppingList: Array(1)}requestKey: "whatever"shoppingList: Array(1)0: nulllength: 1[[Prototype]]: Array(0)[[Prototype]]: Object
let myObj = {
    "requestKey": "whatever",
    "shoppingList": []
};
undefined
myObj
{requestKey: 'whatever', shoppingList: Array(0)}requestKey: "whatever"shoppingList: [][[Prototype]]: Object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文