Json.net 常用使用小结(推荐)

2019-05-22 15:45:02王冬梅

测试效果:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testjson.aspx.cs" Inherits="microstore.testjson" %>

<!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></title>
  <style type="text/css">
    body{ font-family:Arial,微软雅黑; font-size:14px;}
    a{ text-decoration:none; color:#333;}
    a:hover{ text-decoration:none; color:#f00;}
  </style>
</head>
<body>  
  <form id="form1" runat="server">
    <h3>序列化对象</h3>
    表现1:<br />
    <%=TestJsonSerialize()%>
    <%=TestListJsonSerialize() %>
    表现2:<br />
    <%=TestListJsonSerialize2() %>
    <hr />
    <h3>反序列化对象</h3>
    <p>单个对象</p>
    <%=TestJsonDeserialize() %>
    <p>多个对象</p>
    <%=TestListJsonDeserialize() %>  
    <p>反序列化成数据字典Dictionary</p>
    <%=TestDeserialize2Dic() %>
    <hr />  
    <h3>自定义反序列化</h3>
    <%=TestListCustomDeserialize()%>
    <hr />
    <h3>序列化输出的忽略特性</h3>
    NullValueHandling特性忽略=><br />
    <%=CommonSerialize() %><br />
    <%=IgnoredSerialize()%><br /><br />
    属性标记忽略=><br />
    <%=OutIncluded() %><br />
    <%=OutIncluded2() %>
    <hr />
    <h3>Serializing Partial JSON Fragments</h3>
    <%=SerializingJsonFragment() %>
    <hr />
    <h3>ShouldSerialize</h3>
    <%=ShouldSerializeTest() %><br />
    <%=JJJ() %><br /><br />
    <%=TestReadJsonFromFile() %>
  </form>
</body>
</html>

显示:

序列化对象

表现1:

 

{ "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": null }

[ { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] }, { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] } ]

 表现2:

[ { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] }, { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] } ]

反序列化对象

单个对象

•Apple
•2014-05-03 10:20:59
•3.99
•Small,Medium,Large

多个对象

•Apple
•2014-05-03 10:20:59
•3.99
•Small,Medium,Large

•Apple
•2014-05-03 10:20:59
•3.99
•Small,Medium,Large