Json is generally used for a small amount of data processing. The format is simple and easy to operate, and javascript supports the processing of Json format. Therefore, we recommend that you use
The Json format is as follows: {"id": "1", "name": "abc"} or [{"id": "1", "name ": "abc" },{ "id": "1", "name": "abc"}]
Below are several methods to explain Json format in js:
- eval('('+Json+')')
Why do you write this statement in JavaScript? The expression statement cannot start with braces ({). If you do this, it will be confused with the block statement. to solve this problem, parentheses must be added when eval () is used to parse JSON text. parentheses are used as grouping operators to search for expressions enclosed in parentheses.
- var strJSON= (new Function("return " + Json))();
Use the creation method to explain Json
- var strJSON= JSON.parse(Json);
Note: Double quotation marks must be added for key-value pairs in Json format. Otherwise, Json format cannot be interpreted.
Instance
Json. aspx page
- <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Json. aspx. cs" Inherits = "web. javascript. Json. Json" %>
- <! DOCTYPE html>
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head runat = "server">
- <Title> </title>
- </Head>
- <Body>
- <Form id = "form1" runat = "server">
- <Div id = "div1">
- </Div>
- <Input type = "button" id = "btn" value = "but"/>
- </Form>
- <Script type = "text/javascript" src = "http://www.cnblogs.com/script/jquery-1.7.1.min.js"> </script>
- <Script type = "text/javascript">
- // Process Json objects Asynchronously
- $ ("# Btn"). click (function (){
- $. Ajax ({
- Url: "Json. ashx ",
- Type: "post ",
- Data: {id: "123 "},
- Datatype: "Json ",
- Success: function (data ){
- Var strData = data;
- Alert ("1 -----------");
- Var str1 = eval ("(" + data + ")"); // The first js interpretation Json
- Alert ("2 -----------");
- Var str2 = (new Function ("return" + strData) (); // The second js interpretation Json
- Alert (str2.id + "@" + str2.name );
- Alert ("3 -----------");
- Var str3 = JSON. parse (strData );
- Alert (str3.id + "@" + str3.name );
- // Process multi-dimensional Json
- Var strSs = "";
- Alert (str1.length );
- For (var I = 0; I <str1.length; ++ I ){
- StrSs + = str1 [I]. id + "@" + str1 [I]. name + "";
- }
- Alert (strSs );
- },
- Error: function (xhr, data, ts ){
- Alert (data );
- }
- });
- });
- </Script>
- </Body>
- </Html>
- Json. ashx Handler
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Web;
- Using System. Web. Services;
- Namespace web. javascript. Json
- {
- /// <Summary>
- /// $ Codebehindclassname $ abstract description
- /// </Summary>
- [WebService (Namespace = "http://tempuri.org/")]
- [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
- Public class Json1: IHttpHandler
- {
- Public void ProcessRequest (HttpContext context)
- {
- Context. Response. ContentType = "text/plain ";
- // Multidimensional
- String strJSONs = "[{\" id \ ": 1, \" name \ ": \" 11 \ "},{ \" id \ ": 2, \ "name \": \ "22 \" },{ \ "id \": 3, \ "name \": \ "33 \"}] ";
- // One-dimensional
- // String strJSONs = {\ "id \": \ "123 \", \ "name \": \ "qwe \"}
- Context. Response. Write (strJSONs );
- Context. Response. End ();
- }
- Public bool IsReusable
- {
- Get
- {
- Return false;
- }
- }
- }
- }
The above is my summary in the actual project. I hope to help you and give more advice.
Address of this Article:Http://www.cnblogs.com/snakeraining/archive/2012/03/26/2418519.html