2

我有一个复杂的对象如下

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;}
}

其中 Success- 指示操作是否成功执行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":""}}]
4

1 回答 1

2

如果您不想使用第三方 JSON 库,请选择DataContractJsonSerializer. 请参阅MSDN 参考资料

如果您期望 List 在 Data 字段中被序列化:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Cart>));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Data));
List<Cart> carts = ser.ReadObject(ms) as List<Cart>;
于 2013-09-26T09:11:29.493 回答