1

I am processing JSON responses (displayed below) from a service on my Windows Phone 7 client. I am using Json.NET to deserialize them into an object, which contains List of products.

But after I deserialize, when I looked into my serviceresponse object, I can see a list of 2 products. But when I expand the product object, the fields under product (Name, ExpiryDate... etc ) are all null.

I guess my problem is with the way I have defined my serviceresponse class. Can someone help me to resolve the issue and get the correct output.

My deserialization code:

serviceresponse deserializedProduct = JsonConvert.DeserializeObject<serviceresponse>(json);

My Json response String:

{ "serviceresponse" : 

{ "company" : "ford", "success" : "Yes", "products" : [
  {"product" : 

      {
        "Name": "Product 1",
        "ExpiryDate": "\/Date(978048000000)\/",
        "Price": "99.95",
        "Sizes": "1"
      }
  },
  {"product" : 
      {
        "Name": "Product 2",
        "ExpiryDate": "\/Date(1248998400000)\/",
        "Price": "12.50",
        "Sizes": "1"
      }
  }
], "callbackformore" : "No", "message" : "1" 

    } 
}

My serviceresponse class:

[DataContract]
public class serviceresponse
{
    [DataMember]
    public String company;
    [DataMember]
    public String success;
    [DataMember]
    public List<product> products;
    [DataMember]
    public String callbackformore;
    [DataMember]
    public String message;
}

[DataContract]
public class product
{
    [DataMember]
    public String Name;
    [DataMember]
    public String ExpiryDate;
    [DataMember]
    public String Price;
    [DataMember]
    public String Sizes;
}

2 Answers 2

3

Remove "product" object names from your json, as this is just array with no named items.

{ "serviceresponse" : 

{ "company" : "ford", "success" : "Yes", "products" : [
  {
    "Name": "Product 1",
    "ExpiryDate": "\/Date(978048000000)\/",
    "Price": "99.95",
    "Sizes": "1"
  },      
  {
    "Name": "Product 2",
    "ExpiryDate": "\/Date(1248998400000)\/",
    "Price": "12.50",
    "Sizes": "1"
  }
], "callbackformore" : "No", "message" : "1" 

} 

}
Sign up to request clarification or add additional context in comments.

4 Comments

Json.NET is adding the extra product class so that it knows how to deserialize to List<T>. If the list wasn't a generic type, the product you're referring to should disappear.
Is there anything that I can do without modifying the response message that I receive from the server?
@fabraham you could write something like this: public class ProductContainer{public product product;} to wrap your original products, and replace products field in serviceresponse like this: public List<ProductContainer> products;. Though I'm not sure this is the best solution, perhaps there are some deserialization flags, or something.
Using Product[] rather than List<Product> would certainly change the deserialization.
2

I ended up doing this. I found this awesome tool Jsonclassgenerator. It could generate C# classes for the input Json string. Then I opened the generated class and found how the classes are laid out. And modified my Datacontract accordingly.

In my case, I have to create another data contract surrounding the list of objects. So it should be like this.

[DataContract]
public class serviceresponse
{
    [DataMember]
    public String company;
    [DataMember]
    public String success;
    [DataMember]
    public List<product> products;
    [DataMember]
    public String callbackformore;
    [DataMember]
    public String message;
}
[DataContract]
public class product
{
    [DataMember(Name = "product")]
    public product2 _product;
}
[DataContract(Name = "product")]
public class product2
{
    [DataMember]
    public String Name;
    [DataMember]
    public String ExpiryDate;
    [DataMember]
    public String Price;
    [DataMember]
    public String Sizes;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.