gjdev.tech
gjdev.tech

Reputation: 135

parsing serialized data back to JSON

I have a complex object as follow

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

My service returns a serialized json of Object ServiceResponse

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

where Success- indicate whether the operation is performed successfully or not
           Data- is the serialized list of object Cart
I am able to parse the Service Response.Now the problem is How to parse the 'Data' part in wp7? (without using JSON.net)

I am trying to parse following data

 [{"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":"[email protected]","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":"[email protected]","City":"","State":"","Country":""}}]

Upvotes: 2

Views: 60

Answers (1)

Lev
Lev

Reputation: 3924

If you don't want to use third party JSON libraries, go for DataContractJsonSerializer. See MSDN reference.

If you are expecting List is serialized in Data field:

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

Upvotes: 2

Related Questions