当前位置:编程学习 > JS >>

json学习笔记

JSON:JavaScript 对象表示法(JavaScript Object Notation)。
JSON 是存储和交换文本信息的语法。类似 XML。
JSON 比 XML 更小、更快,更易解析。
实例:
  View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="json_test.aspx.cs" Inherits="Web_SoftAceTest.json.json_test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        var text = '{employees:[' +
     '{"LastName":"Getes","FirstName":"Bill"},' +
     '{"LastName":"Yue","FirstName":"Ace"},' +
     '{"LastName":"Bush","FirstName":"George"}]}';
        var obj = eval("(" + text + ")");
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>
    Name:<span id="sp_name"></span><br />
    Age:<span id="sp_age"></span><br />
    Address:<span id="sp_address"></span><br />
    Phone:<span id="sp_phone"></span>
    </p>
   
    <script type="text/javascript">
        var JsonObject = { employees: [
        { "Name": "AceYue",
            "Age": "22",
            "Address": "Guangdong Shenzheng",
            "Phone": "1234567"
        },
        {
            "Name": "Getes",
            "Age": "52",
            "Address": "Americ",
            "Phone": "555-1234567"
        }
            ]
        };

    document.getElementById("sp_name").innerHTML = JsonObject.employees[1].Name;
    document.getElementById("sp_age").innerHTML = JsonObject.employees[1].Age;
    document.getElementById("sp_address").innerHTML = JsonObject.employees[1].Address;
    document.getElementById("sp_phone").innerHTML = JsonObject.employees[1].Phone;
    </script>
   
    <p>
    FirstName:<span id="sp_firstname"></span><br />
    LastName:<span id="sp_lastname"></span>
    </p>
    <script type="text/javascript">
        document.getElementById("sp_firstname").innerHTML = obj.employees[0].FirstName;
        document.getElementById("sp_lastname").innerHTML = obj.employees[0].LastName;
    </script>
    </div>
    </form>
</body>
</html>

 类似 XML
• JSON 是纯文本
• JSON 具有“自我描述性”(人类可读)
• JSON 具有层级结构(值中存在值)
• JSON 可通过 JavaScript 进行解析
• JSON 数据可使用 AJAX 进行传输
相比 XML 的不同之处
• 没有结束标签
• 更短
• 读写的速度更快
• 能够使用内建的 JavaScript eval() 方法进行解析
• 使用数组
• 不使用保留字
JSON 值可以是:
• 数字(整数或浮点数)
• 字符串(在双引号中)
• 逻辑值(true 或 false)
• 数组(在方括号中)
• 对象(在花括号中)
• null
json序列化与反序列化
json的序列化需要用到DataContractJsonSerializer类,在命名空间System.Runtime.Serialization.Json;下。.NET Framework 3.5需要添加System.ServiceModel.Web引用;.NET Framework 4在System.Runtime.Serialization中。
jsonhelper类:
  View Code
 public class JsonHelper
    {
        /// <summary>
        /// json序列号
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string JsonSerializa<T>(T t)
        {
            DataContractJsonSerializer zer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            zer.WriteObject(ms, t);
            string jsonstring = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonstring;
        }

        /// <summary>
        /// json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strjson"></param>
        /// <returns></returns>
        public static T JsonDeserializa<T>(string strjson)
        {
            DataContractJsonSerializer zer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strjson));
&

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,