Full manual for JSON learning-graphic _ javascript skills

Source: Internet
Author: User
Full manual text for JSON Learning Chapter 1 What is JOSN
JSON: JavaScript Object Notation

It is a data structure that describes object data (various metadata is actually the simplest object) in Javascript (such as jscript and ECMA Javascript. The concept and usage are similar to Hash (Hash table) or associated array.

JSON is constructed in two structures:

A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).
An ordered list of values ). In most languages, it is understood as an array ).
These are common data structures. In fact, most modern computer languages support them in some form. This makes it possible to exchange a data format between programming languages that are also based on these structures.

The simplest JSON example is as follows:

The Code is as follows:


[
{Name: "Michael", email: "17bity@gmail.com", homepage: "http://www.jb51.net "},
{Name: "John", email: "john@gmail.com", homepage: "http://s.jb51.net "},
{Name: "Peggy", email: "peggy@gmail.com", homepage: "http://tools.jb51.net"}]



The following example describes a mail contact list. When javascript is run with eval, A maillist object entity is obtained, and javascript can be used for further operations.

The Code is as follows:


Var maillist = [
{Name: "Michael", email: "17bity@gmail.com", homepage: "http://www.jb51.net "},
{Name: "John", email: "john@gmail.com", homepage: "http://s.jb51.net "},
{Name: "Peggy", email: "peggy@gmail.com", homepage: "http://tools.jb51.net "}
]


You can use javascript For further operations.

Chapter 2 JSON format

The basic JSON format is as follows:
An object is a set of attributes and value pairs. An object starts with "{" and ends "}". Each attribute name and value is separated by a ":" prompt, and.



An array is a set of ordered values. An array starts with "[", ends with "]", and values are separated.



The definition of a string is basically the same as that of C or Java.


The definition of numbers is also basically the same as that of C or Java.



Chapter 3 Use trimpath Template to create a JSON Template

Trimpath Template is a lightweight JS code used to parse Json data and generate webpage code.

Demo: http://demo.jb51.net/js/js_template/test.htm

Trimpath. template is JavaScript-based, cross-browser, and adopts the open source code protocol of APL/GPL, which allows you to easily implement a pure JS Engine Based on template programming.

Http://code.google.com/p/trimpath/

It has the following features:

1. It is written in standard JavaScript and supports cross-browser
2. The template syntax is similar to FreeMarker, Velocity, and Smarty.
3. Use a simple language to describe large-segment strings and Dom/DHTML operations
4. You can easily parse the data in the XML file format to the specified template.

This engine can be used to completely handle View tasks. The server Module can directly output Data. Making your MVC model integrated, and because the View is handled by the browser, greatly reducing the burden on the server, it is a good choice to build the Network Information System Application of Ajax technology.

This is a simple and small template tool class to help users quickly assemble the required strings.

Simple Description #{... /} data is executed as js scripts, and the VAR keyword indicates the value of seed (a variable value passed in from outside ).
The template is divided into two parts for execution. The part between compile and run. # {.../} is executed during run,
The part between #{#.../} cannot be passed into seed.
I don't need to write more instructions, because it is really simple.

Principle: it is actually to use the input parameters #{.... (#{... ).


If you are interested in the template technology, you can study the code to get more inspiration.

Summary

As the default JAVASCRIPT data format, json is very convenient for Ajax operations and much smaller than XML files. Therefore, when the network speed is not ideal and the coupling degree of the system is relatively high, the performance is better.

However, XML is a self-descriptive format and is widely used in the world. So when the system requires cross-platform, cross-language, XML is still the best choice.

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.