JSON objects and arrays and simple implementations for converting to JS objects _javascript tips

Source: Internet
Author: User

JSON Syntax rules

The JSON syntax is a subset of the JavaScript object representation syntax.

• Data in name/value pairs
• Data separated by commas
• Curly Braces Save objects
• square brackets Save array

JSON Object

The JSON object is written in curly braces:

An object can contain multiple name/value pairs:

 
 

This is also easy to understand and is equivalent to this JAVASCRIPT statement:

FirstName = "John"
lastName = "Doe"

JSON Array

The JSON array is written in square brackets:

Arrays can contain multiple objects:

{" 
employees": [ 
{"FirstName": "John", "LastName": "Doe"}, 
{"FirstName": "Anna", "LastName": "Smith"},< c12/>{"FirstName": "Peter", "LastName": "Jones"} 
] 
} 

In the example above, the object "Employees" is an array that contains three objects. Each object represents a record of someone (with a first and last name).

JSON file

The file type of the JSON file is ". JSON"
The MIME type of the JSON text is "Application/json"

JSON text converted to JavaScript object

The JavaScript function eval () can be used to convert JSON text to JavaScript objects.

The eval () function uses the JavaScript compiler, which parses JSON text and then generates a JavaScript object. You must enclose the text in parentheses to avoid grammatical errors:
var obj = eval ("+ Jsontxt +"));

Example:

  $.ajax ({
    type: ' POST ',
    URL: '.. /.. /casehandler.ashx?action=getcase&id= ' + id.tostring (),//url action is the name of the method
    data: "",
    dataType: "Text",// can be text, if you use text, the result returned is a string, and if you need JSON format, you can set to JSON
    ContentType: "Application/json;" Charset=utf-8 ",
    success:function (returneddata) {
      getmarkerfeature (eval (" + returneddata+ ")));
    error:function (msg) {
      alert ("Access failed: + MSG";
    }
  });

Creating an array of objects from JavaScript

var employees = [
{"FirstName": "Bill", "LastName": "Gates"},
{"FirstName": "George", "LastName": "Bush"},
{"FirstName": "Thomas", "LastName": "Carter"}
];

Two ways to access JavaScript object properties

Object.attribute

Object["attribute"]

For example:

var employees = [
{"FirstName": "Bill", "LastName": "Gates"},
{"FirstName": "George", "LastName": "Bush"},
   
    {"FirstName": "Thomas", "LastName": "Carter"}
];
alert (employees[0].lastname); Way one
alert (employees[0]["LastName"]);//Mode Two
   

The above is a small series for everyone to bring the JSON object and array and convert to a JS object of the simple implementation of the whole content, I hope that we support cloud Habitat Community ~

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.