Json.NET:使用对象列表进行反序列化
我正在处理来自 Windows Phone 7 客户端上的服务的 JSON 响应(如下所示)。我正在使用 Json.NET 将它们反序列化为一个对象,其中包含产品列表。
但是在我反序列化之后,当我查看我的服务响应对象时,我可以看到 2 个产品的列表。但是当我展开产品对象时,产品下的字段(名称、到期日期...等)均为空。
我想我的问题在于我定义服务响应类的方式。有人可以帮助我解决问题并获得正确的输出吗?
我的反序列化代码:
serviceresponse deserializedProduct = JsonConvert.DeserializeObject<serviceresponse>(json);
我的 Json 响应字符串:
{ "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"
}
}
我的服务响应类:
[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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 json 中删除
"product"
对象名称,因为这只是没有命名项的数组。Remove
"product"
object names from your json, as this is just array with no named items.我最终这样做了。我发现了这个很棒的工具 Jsonclassgenerator。它可以为输入 Json 字符串生成 C# 类。然后我打开生成的类并发现类是如何布局的。并相应地修改了我的数据合同。
就我而言,我必须围绕对象列表创建另一个数据协定。所以应该是这样的。
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.