Question

A valid JSON Syntax is something of the kind:

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

But what if I want to transmit an array of 'users' (given the example), instead of a single 'user' ?

Is the code below Valid JSON, according to the specifications?

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

And if not, what is the best way to transmit an array of values across with JSON? (And with 'best way', I mean syntactically)

Was it helpful?

Solution

Yes, your example is valid JSON - that is exactly how you want to use an array.

Edit : Here is a good link on JSON and its usage.

OTHER TIPS

The not-very-well-known page json.org has a diagram that shows the syntax. It’s extremely simple to understand, IMHO.

Json Syntax Includes following.

 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 Objects Example

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

Json Array Example

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

I have taken reference from http://www.tutsway.com/json-syntax.php

What you wrote up there is already correct :)

[{ "username" : "admin", "password" : "123" }, { "username" : "bbvb", "password" : "sdfsdf" }, { "username" : "asd", "password" : "222" }]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top