JSON data has the following form
Object: Objects can also be nested within objects, and the name can only be represented by String
Form: {name/value}
Example: {"Math Score":95,"Chinese Score":89}
Array: an ordered collection of values
Format: [value], separated by commas
Example: [1,4,5]
Value: can be string, numeric, boolean, object, array
String: A collection of any number of Unicode characters surrounded by double quotes
Example: "hello world"
Assign JSON data to a variable:
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" }, { "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] } |
How to access data:
people.programmers[0].lastName; |
How to modify data :
people.musicians[1].lastName = "Rachmaninov"; |
Convert JSON data to string
String newJSONtext = people.toJSONString();
Convert any JavaScript object to JSON text
The structure of JSON has only 2 forms: object, array
JSON values can be: numbers, objects, arrays, strings, booleans