jQuery JSON 字符串与 PHP json_encode
我正在查看我的数据库,有些地方使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
[]
用于创建一个简单数组,其中{}
创建一个“对象”,在本例中用作关联数组。第一个示例将值 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.
这是一个文字,表示具有一个元素的
Object
,其键为"0"
,答案为"33"
。这是一个表示具有三个元素的数组的文字,其值在代码中显而易见。
jQuery 与此无关。这是 Javascript 语法,这就是 JSON 代表 JavaScript 对象表示法的原因。
This is a literal that represents an
Object
with one element, whose key is"0"
and whose answer is"33"
.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.
第一个是关联数组(键值对,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.
如上所述,json 的区别在于:
[ ] 是数组,只接受值
{ } 是对象,同时接受 key 和 value
它们可以如下组合在一起:
所以下次你可以自己弄清楚如何组合它: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:
so next time you can figure out how to compose it yourself :D