且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Json.NET:使用对象列表进行反序列化

更新时间:2023-01-17 09:24:08

我最终这样做了.我发现了这个很棒的工具 Jsonclassgenerator .它可以为输入的Json字符串生成C#类.然后,我打开生成的类,并发现如何布置这些类.并相应地修改了我的Datacontract.

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