最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON array syntax. Are the curly brackets (object syntax) strictly necessary? - Stack Overflow

programmeradmin1浏览0评论

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 ) .

Parsing the json string will allways make an object from the string ? or is it possible to parse the string into an array ?

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.

As requested here is the tag that holds the json string :

<button data-fproc='[["state","accepted"],["r_by_uid","1"]]' class="request_state_button">

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 ) .

Parsing the json string will allways make an object from the string ? or is it possible to parse the string into an array ?

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.

As requested here is the tag that holds the json string :

<button data-fproc='[["state","accepted"],["r_by_uid","1"]]' class="request_state_button">
Share edited Jul 10, 2012 at 13:54 Tudor asked Jul 10, 2012 at 13:36 TudorTudor 1,1812 gold badges13 silver badges28 bronze badges 3
  • Paste the html as it appears in view source, like: data-fproc="example" – Esailija Commented Jul 10, 2012 at 13:48
  • Works fine here jsfiddle/7ARmP/1 Review any possible differences such as calling .attr when the element doesn't exist – Esailija Commented Jul 10, 2012 at 13:57
  • @Esailija there must be something wrong with the jquery then ... anyway ty to all who helped , basically the question has a valid answer , i'll find the problem eventually – Tudor Commented Jul 10, 2012 at 13:59
Add a ment  | 

2 Answers 2

Reset to default 3

Curly brackets are not strictly necessary.

[["state","accepted"],["r_by_uid",1]] is valid JSON.

A JSON text can be an object or an array.

See http://json/ and the JSON Grammar section in https://www.ietf/rfc/rfc4627.txt

You can validate your JSON at http://jsonlint./


In Javascript, JSON.parse() returns an array:

JSON.parse('[["state","accepted"],["r_by_uid",1]]')
// result [["state", "accepted"], ["r_by_uid", 1]]

Notice that Arrays are also objects in Javascript.

It works with jQuery.parseJSON() too:

jQuery.parseJSON('[["state","accepted"],["r_by_uid",1]]')
// result [["state", "accepted"], ["r_by_uid", 1]]

Probably this will explain your problem:

 var aAsArray = [["state","accepted"],["r_by_uid",1]];
 var aAsString = '[["state","accepted"],["r_by_uid",1]]';

 ​JSON.parse(aAsArray);​​​    //Uncaught SyntaxError: Unexpected token ....
 JSON.parse(aAsString);
发布评论

评论列表(0)

  1. 暂无评论