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 ~