JavaScript: Basic Extensions (1)--json

Source: Internet
Author: User

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

  • JSON is typically used to exchange data with the server.
  • is typically a string when sending data to the server.
  • We can use the Json.stringify () method to convert a JavaScript object to a string.
  • Json.stringify (value[, replacer[, space])

  

JavaScript: Basic Extensions (1)--json

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.