如何使用json.net&amp ;;序列化子项目C#

时间:2013-05-09 02:32:36

标签: json.net subitem

我如何使用json.net&序列化? C#? 什么是最好的方式? 如何追加子节点? 像这样输出json字符串:

{ “OptQty”:100, “OptQtySpecified”:假的, “OptTextList”: [{ “SiteStockTextNo”:1634245835, “DescriptiveText”: “文字样式”, “DisplayIs”:真, “一changeType”:1}], “OptSel”:{ “OptRepImageLevel”:0 “OptRepImageLevelSpecified”:真 “SortOrder的”:1, “ObjOptInfo”:{ “ObjOptNo1”:0 “ObjOptClaseNm1”: “fileds1”, “ObjOptClaseNm5”: “”}, “计算”:空, “StockList”: [ { “SiteStockNo”:1633316906, “text1” 中的: “ABCD”, “文本2”: “PK7701”, “数量”:0, “价格”:14500, “SoldOutIs”:真正}, { “SiteStockNo”:1633316907, “文本1”: “ABCD”, “文本2”: “PK7702”, “数量”:0, “价格”:12500 “SoldOutIs”:假}, { “SiteStockNo”:1633316908, “文本1”: “ABCD”, “文本2”: “PK7703”, “数量”:0, “价格”:12500 “SoldOutIs”:假}, { “SiteStockNo”:1633316915, “文本1”: “ABCD”, “文本2”: “PK7711”, “数量”:0, “价格”:2500, “SoldOutIs”:假} ] }, “OptAdd”:空 }

1 个答案:

答案 0 :(得分:0)

最好的方法是使用System.Web.Script.Serialization.JavaScriptSerializer

A simple Employee object:

public class Employee
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string ID { get; set; }   
}

Adding some instances of them to a List:

Employee oEmployee1 = 
       new Employee{Name="Pini",ID="111", Age="30"};

Employee oEmployee2 = 
      new Employee { Name = "Yaniv", ID = "Cohen", Age = "31" };
Employee oEmployee3 = 
        new Employee { Name = "Yoni", ID = "Biton", Age = "20" };

List<Employee> oList = new List<Employee>() 
{ oEmployee1, oEmployee2, oEmployee3 };

System.Web.Script.Serialization.JavaScriptSerializer oSerializer = 
         new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(oList);