A summary of Json

Source: Internet
Author: User

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:

 
 
  1. 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.

 
 
  1. var strJSON= (new Function("return " + Json))(); 

Use the creation method to explain Json

 
 
  1. 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

 
 
  1. <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Json. aspx. cs" Inherits = "web. javascript. Json. Json" %>
  2. <! DOCTYPE html>
  3. <Html xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head runat = "server">
  5. <Title> </title>
  6. </Head>
  7. <Body>
  8. <Form id = "form1" runat = "server">
  9. <Div id = "div1">
  10. </Div>
  11. <Input type = "button" id = "btn" value = "but"/>
  12. </Form>
  13. <Script type = "text/javascript" src = "http://www.cnblogs.com/script/jquery-1.7.1.min.js"> </script>
  14. <Script type = "text/javascript">
  15. // Process Json objects Asynchronously
  16. $ ("# Btn"). click (function (){
  17. $. Ajax ({
  18. Url: "Json. ashx ",
  19. Type: "post ",
  20. Data: {id: "123 "},
  21. Datatype: "Json ",
  22. Success: function (data ){
  23. Var strData = data;
  24. Alert ("1 -----------");
  25. Var str1 = eval ("(" + data + ")"); // The first js interpretation Json
  26. Alert ("2 -----------");
  27. Var str2 = (new Function ("return" + strData) (); // The second js interpretation Json
  28. Alert (str2.id + "@" + str2.name );
  29. Alert ("3 -----------");
  30. Var str3 = JSON. parse (strData );
  31. Alert (str3.id + "@" + str3.name );
  32. // Process multi-dimensional Json
  33. Var strSs = "";
  34. Alert (str1.length );
  35. For (var I = 0; I <str1.length; ++ I ){
  36. StrSs + = str1 [I]. id + "@" + str1 [I]. name + "";
  37. }
  38. Alert (strSs );
  39. },
  40. Error: function (xhr, data, ts ){
  41. Alert (data );
  42. }
  43. });
  44. });
  45. </Script>
  46. </Body>
  47. </Html>
  48. Json. ashx Handler
  49. Using System;
  50. Using System. Collections. Generic;
  51. Using System. Linq;
  52. Using System. Web;
  53. Using System. Web. Services;
  54. Namespace web. javascript. Json
  55. {
  56. /// <Summary>
  57. /// $ Codebehindclassname $ abstract description
  58. /// </Summary>
  59. [WebService (Namespace = "http://tempuri.org/")]
  60. [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
  61. Public class Json1: IHttpHandler
  62. {
  63. Public void ProcessRequest (HttpContext context)
  64. {
  65. Context. Response. ContentType = "text/plain ";
  66. // Multidimensional
  67. String strJSONs = "[{\" id \ ": 1, \" name \ ": \" 11 \ "},{ \" id \ ": 2, \ "name \": \ "22 \" },{ \ "id \": 3, \ "name \": \ "33 \"}] ";
  68. // One-dimensional
  69. // String strJSONs = {\ "id \": \ "123 \", \ "name \": \ "qwe \"}
  70. Context. Response. Write (strJSONs );
  71. Context. Response. End ();
  72. }
  73. Public bool IsReusable
  74. {
  75. Get
  76. {
  77. Return false;
  78. }
  79. }
  80. }
  81. }

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

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.