The front and back ends of JSON are uniformly programmed as objects.

Source: Internet
Author: User
Tags tojson
ArticleDirectory
    • Introduction to front-end plug-ins
    • Background tools
    • Front-end JSON reading
    •  
    • JSON processing in the background:
Introduction to front-end plug-ins

Jquery. JSON plugin {jquery plugin}

Main Methods:

$. Tojson (JSON object): converts a JSON object to a string.

$. Evaljson (STR): converts a string to a JSON object.

Plug-in download: jquery. json-2.3.min.js

 

Background tools

Json.net

Main Methods:

Convert an object to a JSON string:

 
User u = new user () {id = 1000, name = "", age = 50}; string returnstr = jsonconvert. serializeobject (U );

Convert a JSON string to an object:

 
User user = jsonconvert. deserializeobject <user> (request ["data"]);

Download: newtonsoft.json.zip

 

Front-end JSON reading

Method jquery:

Jquery. getjson (URL, [Data], [Callback]) Return Value: XMLHttpRequest Parameter URLString

Send request address.

Data(Optional)Map

The key/value parameter to be sent.

Callback(Optional)Function

Callback function when loading is successful.

Example:

1.Two Parameters

Jquery. getjson ("JSON. aspx ", function (JSON) {_ JSON = JSON; $ (" # textarea1 "). val (JSON); $ ("# text1 "). val (JSON. ID); $ ("# text2 "). val (JSON. name); $ ("# text3 "). val (JSON. age );})

2.Three Parameters

Jquery. getjson ("setjson. aspx ", {data: $. tojson (_ JSON)}, function (JSON) {$ ("# text4 "). val (JSON. ID); $ ("# text5 "). val (JSON. name); $ ("# text6 "). val (JSON. age );})

{Data: $. tojson (_ JSON)}: it can be a string or JSON data.

$. Tojson: A method provided by the jquery. JSON plug-in to convert a JSON object to a string.

JSON processing in the background:

Json.net is required

User class:

 
Public class user {private long ID; public long ID {get {return ID;} set {id = value ;}} private string name; Public string name {get {return name ;} set {name = value ;}} private int age; Public int age {get {return age ;}set {age = value ;}}}

Convert an object to a JSON string:

 
User u = new user () {id = 1000, name = "", age = 50}; string returnstr = jsonconvert. serializeobject (U );

Convert a JSON string to an object:

 
User user = jsonconvert. deserializeobject <user> (request ["data"]);

 

Simple Example of the entire project: 1. Home Page
<Head runat = "server"> <title> </title> <SCRIPT src = "scripts/jquery-1.4.1.min.js" type = "text/JavaScript"> </SCRIPT> <SCRIPT src = "scripts/jquery. json-2.3.min.js "type =" text/JavaScript "> </SCRIPT> <SCRIPT type =" text/JavaScript "> VaR _ JSON; function getserverjson () {jquery. getjson ("JSON. aspx ", function (JSON) {_ JSON = JSON; $ (" # text1 "). val (JSON. ID); $ ("# text2 "). val (JSON. name); $ ("# text3 "). val (JSON. age) ;}} function setserveruser () {_ JSON. id = $ ("# text1 "). val (); _ JSON. name = $ ("# text2 "). val (); _ JSON. age = $ ("# text3 "). val (); jquery. getjson ("setjson. aspx ", {data: $. tojson (_ JSON)}, function (JSON) {$ ("# text4 "). val (JSON. ID); $ ("# text5 "). val (JSON. name); $ ("# text6 "). val (JSON. age );})} </SCRIPT> 

Click "Get JSON" to get the user object from JSON. aspx.

Click "modify" to upload the modified object in the first row to setjson. aspx, restore the object, and then return the restored object, which is displayed in the second row of data on the page.

2. JSON. aspx
 
Protected void page_load (Object sender, eventargs e) {user u = new user () {id = 1000, name = "", age = 50}; returnstr = jsonconvert. serializeobject (U );}
3. setjson. aspx
 
Protected void page_load (Object sender, eventargs e) {user = jsonconvert. deserializeobject <user> (request ["data"]); returnstr = jsonconvert. serializeobject (User );}

Download project source code:Json.zip

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.