0

我已经设法通过创建数据合同类来创建反序列化(在向客户唠叨一公吨之后)。我的问题是我声明的两个字段都返回空的东西。因此,我查看了它并意识到 JSON 对象是嵌套的,我不清楚如何访问其中的部分。

数据合同是这样的,但我将Bopp设为 null(或空字符串,不确定是哪个),并将Mopp设为一堆零。

[DataContract]
public class Customer
{
  [DataMember(Name = "Beep")]
  public String Bopp;

  [DataMember(Name = "Meep")]
  public Guid Mopp;
}

以为数据在这个表格上。

[
  {"Beep":"beep1", "Meep":"meep1"},
  {"Beep":"beep2", "Meep":"meep2"},
  {"Beep":"beep3", "Meep":"meep3"}
]

然而,显然,他们将对象移动到另一个对象中,所以它更像这样。

[ "root":[
  {
    "A":"some",
    "B":[
      {"Beep":"beep1", "Meep":"meep1"},
      {"Beep":"beep2", "Meep":"meep2"},
      {"Beep":"beep3", "Meep":"meep3"}],
    "C":"some"
  },
  {
    "A":"some",
    "B":[
      {"Beep":"beep1", "Meep":"meep1"},
      {"Beep":"beep2", "Meep":"meep2"},
      {"Beep":"beep3", "Meep":"meep3"}],
    "C":"some"
  }
]]

如何重新设计数据合约以使其访问正确的字段?还是我遗漏了什么,数据成员的名称不能与字段不同(即BeepBopp不起作用)?!

编辑:

根据要求,我正在从字符串中发布(几乎)实时数据。

{"CustomerStatuses":[{
  "Information":[{"Guid":"1","Role":"Customer"}],
  "CustomerId":"12345678-1234-1324-1234-123456781234",
  "Status":4},
  "Information":[{"Guid":"5","Role":"Customer"}],
  "CustomerId":"12345678-1234-1324-1234-123456781234",
  "Status":6},
  "Information":[{"Guid":"7","Role":"Seller"}],
  "CustomerId":"12345678-1234-1324-1234-123456781234",
  "Status":6},
  ...

这是实际的数据合同。

[DataContract]
public class Customer
{
  [DataMember(Name = "Status")]
  public String Status;

  [DataMember(Name = "CustomerId")]
  public Guid Guid;
}
4

1 回答 1

0

您的类结构需要匹配 Json:

<DataContract> 
Public Class Customer 

<DataMember(Name:="Status")> 
Public Property Status As Int32 

<DataMember> 
Public Property Information As Object 

<DataMember> 
Public Property CustomerId As String 

End Class 




Public Class Customers 

Public Property CustomerStatuses As List(Of Customer) 

End Class
于 2013-02-08T17:35:39.020 回答