Json.NET: Deserialization with list of objects

99封情书 提交于 2019-12-08 05:10:02

问题


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

回答1:


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" 

} 

}



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


来源:https://stackoverflow.com/questions/7523504/json-net-deserialization-with-list-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!