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)
Yes, your example is valid JSON - that is exactly how you want to use an array.
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" }]
来源:https://stackoverflow.com/questions/431494/json-syntax-transmitting-an-array