JavaScript: Extending Knowledge (1)--json
Understanding:
The most important thing about JSON is to understand that it is a data format, not a programming language . Although it has the same syntactic form, JSON does not belong to JavaScript. Also, JSON is not only used by JavaScript, after all, JSON is just a data format. Many programming languages have parsers and serializers for JSON.
- The file type of the JSON file is ". JSON"
- The MIME type of JSON text is "Application/json"
Syntax:JSON fetch value
- Number (integer or floating point)
- String (in double quotes)
- Logical value (TRUE or FALSE)
- Array (in brackets)
- Object (in curly braces)
- Null
{"Age": "Flag":true"Runoob":null }
Object
Description
The 1.JSON object is written in {}.
2. An object can contain multiple key/value (key/value) pairs.
3.key must be a string and value must be a valid JSON data type.
Use colons (:) split in 4.key and value. Each key/value pair uses commas (,) to split.
Object notation:
/* JSON Object Notation */ { "name":"MS", "age": +, "id": 10010}
Object array:
/* JSON Array */ [ {"title": "A", "num": Ten}, {"title": "B", "num": +}, {"title": "C", "num": 30 }]
To access the object value:
/* use the. Access Object Value */ var = {"Name": "Runoob", "Alexa": 10000, "site":null= myobj.name; /* accessing object values using [] */ var = MYOBJ = {"Name": "Runoob", "Alexa": 10000, "site":null
x = myobj["name"];
Using loops to traverse an object
var myObj = {"Name": "Runoob", "Alexa": 10000, "site":null }; for inch MYOBJ) { document.getElementById ("Demo"). InnerHTML + = Myobj[x] + "<br>";}
Nested JSON objects
One JSON object nesting another JSON object
MYOBJ = { "name": "Runoob", "Alexa": 10000, "Sites": { "Site1": "www.runoob.com", "site2": "m.runoob.com", "site3": "C.runoob.com" }}
To access nested JSON objects
x = myObj.sites.site1; // or x = myobj.sites["Site1"];
modifying values
myObj.sites.site1 = "www.google.com"; myobj.sites["site1"] = "www.google.com";
Delete Object Properties
Delete myObj.sites.site1; Delete myobj.sites["site1"]
parsing and serialization
parsing
Description
- JSON is typically used to exchange data with the server.
- is typically a string when receiving server data.
- We can use the Json.parse () method to convert data to JavaScript objects.
Receiving JSON data from the server (http://www.runoob.com/json/json-parse.html)
Use AJAX to request JSON data from the server and parse it into a JavaScript object.
var New function() { if (this this. Status = =) { = Json.parse (this. responsetext); document.getElementById ("Demo"). InnerHTML = myobj.name; }}; Xmlhttp.open (true); Xmlhttp.send ();
Serialization of
Description
JavaScript: Basic Extensions (1)--json