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

发布于 2024-12-05 23:41:01 字数 1421 浏览 1 评论 0原文

我正在处理来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-12-12 23:41:01

从 json 中删除 "product" 对象名称,因为这只是没有命名项的数组。

{ "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" 

} 

}

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" 

} 

}
笑饮青盏花 2024-12-12 23:41:01

我最终这样做了。我发现了这个很棒的工具 Jsonclassgenerator。它可以为输入 Json 字符串生成 C# 类。然后我打开生成的类并发现类是如何布局的。并相应地修改了我的数据合同。

就我而言,我必须围绕对象列表创建另一个数据协定。所以应该是这样的。

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文