JSON数组语法。大括号(对象语法)是否严格必要? [英] JSON array syntax. Are the curly brackets (object syntax) strictly necessary?
问题描述
json字符串是否可以只包含方括号?对于前者:
Is it possible for a json string to include only square brackets ? For ex. :
[["state","accepted"],["r_by_uid",1]]
我从解析该字符串中得到意外的字符错误...(很长一段时间以来,我在这个脚本上工作,我觉得它之前有效)。
I get unexpected character error from parsing that string ... (long time since i worked on this script and i think it worked before ) .
解析json字符串将总是从字符串中创建一个对象?或者是否可以将字符串解析成数组?
Parsing the json string will allways make an object from the string ? or is it possible to parse the string into an array ?
基本上我只是想把字符串解析成一个数组而不是一个对象。
我用google搜索了一些例子但是找不到任何使用的例子只有方括号。
Basically i just want to parse the string into an array , not an object .
I googled some examples but couldnt find any example that is using only square brackets.
这里要求的是包含json字符串的标记:
As requested here is the tag that holds the json string :
<button data-fproc='[["state","accepted"],["r_by_uid","1"]]' class="request_state_button">
推荐答案
严格要求使用圆括号。
[[state,accepted],[r_by_uid,1]]
是有效的JSON。
JSON文本可以是对象或数组。
A JSON text can be an object or an array.
参见 http://json.org/ 和 JSON Grammar 部分/rfc4627.txtrel =nofollow> https://www.ietf.org/rfc/rfc4627.txt
See http://json.org/ and the JSON Grammar section in https://www.ietf.org/rfc/rfc4627.txt
您可以验证您的JSON在 http://jsonlint.com/
You can validate your JSON at http://jsonlint.com/
在Javascript中, JSON.parse()
返回一个数组:
In Javascript, JSON.parse()
returns an array:
JSON.parse('[["state","accepted"],["r_by_uid",1]]')
// result [["state", "accepted"], ["r_by_uid", 1]]
请注意,数组是也是Javascript中的对象。
Notice that Arrays are also objects in Javascript.
它适用于 jQuery.parseJSON()
:
jQuery.parseJSON('[["state","accepted"],["r_by_uid",1]]')
// result [["state", "accepted"], ["r_by_uid", 1]]
这篇关于JSON数组语法。大括号(对象语法)是否严格必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!