jQuery JSON 字符串与 PHP json_encode

发布于 2024-10-30 16:39:58 字数 219 浏览 1 评论 0原文

我正在查看我的数据库,有些地方使用 jQuery 来制作 JSON 字符串:

{"0":"33"}

然后我看到有些地方使用 PHP json_encode 制作 JSON 字符串,例如:

["News"," world news"," latest news"]

方括号与大括号有区别吗?

I was looking at my database and there were places where jQuery had been used to make a JSON string:

{"0":"33"}

And then I saw places where there were JSON strings made from PHP json_encode like:

["News"," world news"," latest news"]

Do the brackets versus braces make a difference?

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

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

发布评论

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

评论(4

强者自强 2024-11-06 16:39:58

[] 用于创建一个简单数组,其中 {} 创建一个“对象”,在本例中用作关联数组。

第一个示例将值 33 分配给索引 0,但您可以使用任何值作为索引。在第二个示例中,您将创建一个数字索引数组。

[] are used to create a simple array where {} creates an "object" used like an associative array in this case.

The first example assign the value 33 to the index 0, but you can use anything as an index. In the second example, you're creating a number indexed array.

む无字情书 2024-11-06 16:39:58
{"0":"33"}

这是一个文字,表示具有一个元素的 Object,其键为 "0",答案为 "33"

["News"," world news"," latest news"]

这是一个表示具有三个元素的数组的文字,其值在代码中显而易见。

jQuery 与此无关。这是 Javascript 语法,这就是 JSON 代表 JavaScript 对象表示法的原因。

{"0":"33"}

This is a literal that represents an Object with one element, whose key is "0" and whose answer is "33".

["News"," world news"," latest news"]

This is a literal that represents an Array with three elements, whose values are plain to see in the code.

jQuery has nothing to do with it. This is Javascript syntax, which is why JSON stands for JavaScript Object Notation.

衣神在巴黎 2024-11-06 16:39:58

第一个是关联数组(键值对,0 是键,33 是值)。第二个示例是一个包含 3 个位置的列表。它们都已使用 JSON“字符串化”,但数据结构不同。

The first one is an associative array (key-value pair, 0 is the key, 33 is the value). The second example is a list with 3 positions. They have both been "stringified" with JSON but are different data structures.

停滞 2024-11-06 16:39:58

如上所述,json 的区别在于:

[ ] 是数组,只接受值

{ } 是对象,同时接受 key 和 value

它们可以如下组合在一起:

var contact = {
     "Name": "John Doe",
     "PermissionToCall": true,
     "PhoneNumbers": [ 
       {
           "Location": "Home",
           "Number": "555-555-1234"
       },
       {
           "Location": "Work",
           "Number": "555-555-9999 Ext. 123"
       }
     ]
};

所以下次你可以自己弄清楚如何组合它:D

as said above, the difference in json is:

[ ] is array, accepts only value

{ } is object, accepts both key and value

They can be composited together as below:

var contact = {
     "Name": "John Doe",
     "PermissionToCall": true,
     "PhoneNumbers": [ 
       {
           "Location": "Home",
           "Number": "555-555-1234"
       },
       {
           "Location": "Work",
           "Number": "555-555-9999 Ext. 123"
       }
     ]
};

so next time you can figure out how to compose it yourself :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文