JS inside the JSON data operation

Source: Internet
Author: User
Tags object serialization string to json tojson

JSON (JavaScript Object Notation) is a lightweight data interchange format that takes a completely language-independent text format. Bloggers remember a few years ago in the Huawei Outsourcing project there is a need to interact with Android applications, Android calls C # WebService interface, is through the format of JSON to pass the data. Because of this completely language-independent data format, it is widely used in the front and back of the system. This article is used to record the front-end JS and back-end C # JSON inside some common methods of operation. Note notes, good memory is better than notes ~ ~

JS Front-end JSON operation:

One, JSON data to string:

var myList = [            "Jim"            , age:20}, "Kate", Age:21 },            "Lilei", Age:18  },            "John", Age:14 },            "Lintao", Age:25 }    ];

Method One:

var oString2 = json.stringify (myList);

Results

This method is built into the JSON object in JS. can be used directly.

Method Two:

var oString1 = mylist.tojsonstring ();

This method requires json.js support. Direct use will report an exception.

B. String to JSON object:

Method One:

var oString2 = json.stringify (myList); var oJson1 = eval (oString2);

The eval () method is built-in. can be used directly.

Method Two:

var oJson2 = $.parsejson (oString2);

Need the support of jquery.

Method Three:

var oJson3 = Json.parse (oString2);

Is JS built-in, can be used directly.

Method Four:

var oJson4 = String.parsejson (oString2);

The json.js file needs to be cited.

In summary: In JS inside the JSON variable operation does not need to refer to any file directly using the method has Json.stringify (myList), Json.parse (oString2), eval (oString2).

C # background JSON processing:

In C #, JSON can be understood as an anonymous object, and each key-value pair can be understood as the property and value of the corresponding object. In the back-end JSON This format is typically used for serialization of objects:

 Public voidjsontooo () {stringJsonstr ="{\ "name\": \ "tom\", \ "age\": one}";//Jsonstr as a JSON-formatted stringJavaScriptSerializer JSON =NewJavaScriptSerializer ();//instantiate a class that can serialize dataToJson list = json. Deserialize<tojson> (JSONSTR);//Convert the JSON data to an object type and assign a value to the list            stringName = List.name;//the value of name is Tom. List to point out name}

Then there must be an object that corresponds to it.

publicToJson        {            public string Get set;}  // The name of the property must be the same as the "key" value in the JSON format string. public            string age  getset;}        }

The conversion serialization of this correspondence is a lot to say before. There is not much to say here.

MVC is a good support for JSON data in the background program. Like the controller inside the Jsonresult object

 Public Jsonresult Geteditmodel (string  strtype)        {            varnew list< Dragelementprop>();            ListNew list<pageproperty>();             return Json (new {lstdataattr = lstres, pageattr = lstpageattrs}, Jsonrequestbehavior.allowget);        }

Only Draelementprop and Pageproperty are required to support object serialization, so that objects can be uploaded directly to the front end:

[DataContract] Public classPageproperty {[DataMember] Public stringId {Get;Set; } [DataMember] Public stringWidth {Get;Set; } [DataMember] Public stringHeight {Get;Set; } [DataMember]    }

  

Front-end and back-end communication most of the time when using AJAX is through data interaction with JSON data. This time you need to specify the datatype type as JSON.

function (strURL, Oparams, Funsuccesscallback, Funerrorcallback, funfinishcallback) {        $.ajax ({            " POST ",            Url:strurl,            data:oparams,
       Async:false,//false for blocking requests
       DataType: "JSON", success:funsuccesscallback, error:funerrorcallback, complete : Funfinishcallback }) ;

JS inside the JSON data operation

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.