将序列化数据解析回JSON

时间:2013-09-26 09:04:33

标签: json windows-phone-7

我有一个复杂的对象如下

public class Cart
{
  public int cartID{get; set;}
  public bool IsActive{get; set;}
  public double price{get; set;}
  public List<Items> items{get; set;}
}

public class Item
{
  public int itemID{get; set;}}
  public string itemName{get;set;}
  public double price{get; set;}
}

我的服务返回Object ServiceResponse的序列化json

public class ServiceResponse
{
  public bool Success{get;set;}
  public string Data{get;set;}
}

成功 - 表示操作是否成功执行
      Data-是对象Cart的序列化列表 我能够解析服务响应。现在的问题是如何解析wp7中的'数据'部分? (不使用JSON.net)

我正在尝试解析以下数据

 [{"cartID":1,"customerID":10,"dateCreated":22922680,"amount":3026.00,"Items":[{"itemID":263,"itemName":"Item 02","itemPrice":395.00,"item_qty":"4","total_Price":1580.0000},{"itemID":264,"itemName":"item2","itemPrice":495.00,"item_qty":"4","total_Price":1980.0000}],"CustomerDetails":{"CustomerID":10,"LogonID":null,"FirstName":"test","LastName":null,"FullName":"test customer","Phone1":"12345678","Phone2":"","Email":"abc@123.com","State":"","Country":""}},{"cartID":637,"customerID":10,"dateCreated":22922643,"amount":323.00,"Items":[{"itemID":267,"itemName":"Item01","itemPrice":95.00,"item_qty":"4","total_Price":380.0000}],"CustomerDetails":{"CustomerID":10,"LogonID":null,"FirstName":"test customer","LastName":null,"FullName":"test customer","Phone1":"12345678","Phone2":"","Email":"abc@123.com","City":"","State":"","Country":""}}]

1 个答案:

答案 0 :(得分:2)

如果您不想使用第三方JSON库,请转到DataContractJsonSerializer。请参阅MSDN reference

如果您希望List在数据字段中序列化:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Cart>));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Data));
List<Cart> carts = ser.ReadObject(ms) as List<Cart>;