Json data usage and learning methods

Source: Internet
Author: User
Tags array definition

The previous understanding of json is not very clear, because it is rarely used, so there is no special research. Json is a lightweight data exchange format that can be easily processed in multiple languages. It is also the main data format used by large portal interfaces.

Recently, a project involves operations on json data. Json is actually quite simple.

This article describes how to understand and use json in the development process. If there are any errors, please correct them.

Before learning to process json data, you need to understand the definition of arrays and static objects in js.

Array definition in js

// Define a 1D array, which contains three elements
Var arr = new Array ("1", "2", "3 ″);
// Or
Var arr2 = ["1", "2", "3"]; // define an array []
// When the value is set
Arr [0], arr [1], arr [2] // The value corresponding to "1", "2", "3.
// Js arrays are special. Elements in the array can contain various types of elements.
For example, var arr3 = [["1", "2", "3"], "zhangsan", 2]; // arr3 [0] is an array, arr3 [1] is a struct-type arr3 [2] is an integer

Similar to c #
Var arr = new object [3] {new object [3] {"1", "2", "3"}, "zhangsan", 2 };

// We can easily use the js feature to define data types. As follows:
Var arr4 = [["zhangsan", 18, "male"], ["lisi", 20, "male"], ["wangwu", 22, "female"]; // two-dimensional array
// If you need to use elements in the array, you can use the subscript of the array. Convenient access.
Arr4 [0] [0]; // zhangsan arr4 [1] [0] // lisi
However, only subscript access is allowed. If too much data is nested, it involves multi-dimensional data, which can be very difficult to understand.

Definition of static objects in js

Var PeopleInfo = {id: "1", // replace it with "id". The same name is true: "zhangsan", ShowName: function () {console. log (PeopleInfo. name );}};

Defines a static object PeopleInfo, including the property value id, name, and method ShowName.
In use

Access attribute: PeopleInfo. id, PeopleInfo. name
Access Method: PeopleInfo. ShowName ()

Similar to static classes in c.

Now the array and static objects in js are finished. The following describes the key json

Json is an object and a data format.The json format is as follows:

1. Basic Format

var json1={"id":1,"name":"zhangsan"}

2. nested format

Var json2 = {"id": 1, "name": "zhangsan", "address": {"address1": "address 1", "address2 ": "address 2 "}};

3. array nesting format

Var json3 = {"id": 1, "name": "zhangsan", "address": [{"address1": "address 1" },{ "address2 ": "address 2"}]};

This definition contains an array in json, and an array contains two json files. When accessing a nested element, it is the same as the preceding description. when accessing a nested element, you can
Json3.address [0]. address1 // address 1
Json3.address [0] ["address1"] // address 2

When we access data in json format, we only need to remember,For an array, use a subscript. For an object, use a variable name or an attribute..

The json data we encounter is nothing more than the preceding three types. It may be more complicated. As follows:

Var JsonObject = {"person1": {"id": 1, "name": "zhangsan", "address": {"address1": "Beijing 1 ", "address2": "Beijing 2" }}, "person2": {"id": 2, "name": "lisi", "address": {"address1 ": "Shanghai 1", "address2": "Shanghai 2 "}}};

However, as long as you understand the definition of arrays and objects, it is easy to analyze. It is nothing more than nesting between data formats.
The JsonObject contains two objects: person1, person2, person1, and person2, which have the same structure and contain two variables: id, name, and address. When accessing, you only need to access it in a hierarchical manner. For example
JsonObject ["person1"] ["address"] ["address1"] // Beijing 1

To traverse,Json data format TraversalAs follows:

Function JsonLoop (json) {for (var key in json) {if (json. hasOwnProperty (key) {if (typeof json [key] = "object") {JsonLoop (json [key]);} else {console. log ("key:" + key + ", val:" + json [key]) ;}}// call method JsoanLoop (JsonObject );

By default,. net does not support reading and writing in json data format. If you need to operate this data format, you can separate it for processing.
1. ajax is transmitted in json data format. It is received in ashx and processed as a parameter. The returned value is of the text type.. For example

$. Ajax ({url: "ajax/jsontest. ashx ", data: {" id ": 1," name ":" zhangsan "}, // json data format type: 'post', success: function (data) {alert (data );}});

The code used by ashx can be directly received by the variable name.

int id = int.Parse(context.Request.Form["id"]);string name = context.Request.Form["name"];context.Response.Write(string.Format("id={0},name={1}",id,name));

2.asp.net json Data Operations
Vs2012 supports json serial numbers and deserialization, but earlier versions do not. So I searched from google for the Net. Json class (Open Source ). Json data can be conveniently operated.
: Http://sourceforge.net/projects/csjson? Source = dlp

Net. Json is used as follows:

Function TestJsonAjax () {$. ajax ({url: "ajax/jsontest. ashx ", data: {" id ": 1}, // dataType: 'json' in json data format, // set the data type: 'post', success: function (data) {JsonLoop (data) ;}, // call the traversal function error: function (data, error) {alert (error + ": "+ data )}});}

Then, it receives the id in ashx and returns the josn data type.

Context. response. contentType = "application/json"; int id = int. parse (context. request. form ["id"]); JsonObjectCollection jsonObject = new JsonObjectCollection (); jsonObject. add (new JsonStringValue ("name", "zhangsan"); jsonObject. add (new JsonObjectCollection ("address", new JsonObject [3] {new JsonStringValue ("address1", "Beijing 1"), new JsonStringValue ("address2 ", "Beijing 2"), new JsonStringValue ("address3", "Beijing 3")}); context. response. write (jsonObject. toString ());

 

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.