JSON语法:传输数组

时间:2009-01-10 18:32:27

标签: json serialization data-transfer

有效 JSON语法就是这样的:

{
  "username": "admin",
  "password": "123"
}

但是如果我想传输'用户'数组(给出示例)而不是单个'用户'呢?

根据规范,代码是否为有效JSON?

[{
  "username": "admin",
  "password": "123"
}, {
  "username": "bbvb",
  "password": "sdfsdf"
}, {
  "username": "asd",
  "password": "222"
}]

如果没有,使用JSON传输值数组的最佳方法是什么? (并且用'最好的方式',我的意思是语法上的)

4 个答案:

答案 0 :(得分:11)

是的,您的示例是有效的JSON - 这正是您想要使用数组的方式。

修改Here is a good link on JSON and its usage.

答案 1 :(得分:5)

不太知名的页面json.org有一个显示语法的图表。理解起来非常简单,恕我直言。

答案 2 :(得分:2)

Json语法包含以下内容。

 1. Data is represented in name/value pairs.
 2. Each name is followed by ':'(colon).
 3. The name/value pairs are separated by ,(comma).
 4. Json object starts and ends with '{' and '}'.
 5. Square brackets '[ ]' hold arrays and values are separated by
    ,(comma).

Json对象示例

    {
    "id":"21",
    "language": "Json",
    "edition": "second",
    }

Json数组示例

  {
        "book": [
        {
        "id":"21",
        "language": "Json",
        "edition": "second"
        },
        {
        "id":"42",
        "language": "Json",
        "edition": "third"
        }]
        }

我参考了http://www.tutsway.com/json-syntax.php

答案 3 :(得分:1)

你在那里写的内容已经正确了:)

[{ "username" : "admin", "password" : "123" }, { "username" : "bbvb", "password" : "sdfsdf" }, { "username" : "asd", "password" : "222" }]