如何反序列化嵌套对象? [英] How to deserialize a nested object?

查看:121
本文介绍了如何反序列化嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功通过创建一个数据合同类(唠叨的客户公吨后)来创建一个反序列化。我的问题是,这两个领域我已经宣布,正在返回空的东西。所以,我看了一下,意识到JSON对象是嵌套,我如何进入其内部的部分尚不清楚。



数据合同像这样的,但我得到的波普的为空(或空字符串,不知道哪)和 MOPP 的成一串零。

  [DataContract] 
公共类客户
{
[数据成员(NAME =哔声)]
公共字符串波普;

[数据成员(NAME =MEEP)]
公众的Guid MOPP;
}

我觉得的数据是这样的形式

  [
{哔:BEEP1,MEEP:meep1},
{哔:beep2,MEEP:meep2},
{哔:beep3,MEEP:meep3}
]

不过,很显然,他们把对象的的其他所以它更像是这一点。

  [根:
{
A:一些,
b:
{哔:BEEP1,MEEP:meep1},
{哔:beep2,MEEP:meep2},
{哔:beep3,MEEP:meep3}],
C:一些
},
{
A :一些,
b:
{哔:BEEP1,MEEP:meep1},
{哔:beep2 MEEP:meep2},
{哔:beep3,MEEP:meep3}],
C:一些
}
]

如何重新设计数据合同,使​​其进入正确的领域?还是我失去了一些东西和数据成员的名称不能从外地不同(比如蜂鸣的和的波普的都不行)?!



编辑:



根据要求,我张贴(几乎)从字符串实时数据

  {CustomerStatuses:[{
信息:[{GUID:1,角色:客户} ],
客户ID:12345678-1234-1324-1234-123456781234,
状态:4},
信息:[{GUID:5 ,角色:客户}],
客户编号:12345678-1234-1324-1234-123456781234,
状态:6}
信息: [{GUID:7,角色:卖方}],
客户编号:12345678-1234-1324-1234-123456781234,
状态:6} ,
...

和这里的实际数据的合同。

  [DataContract] 
公共类客户
{
[数据成员(NAME =状态)]
酒店的公共字符串状态;

[数据成员(NAME =客户编号)]
公众的Guid的Guid;
}


解决方案

您阶级结构需要相匹配JSON的:

 < D​​ataContract> 
类公共客户

<数据成员(名称:=状态)>
公共财产状况的Int32

<&数据成员GT;
公共属性信息作为对象

<&数据成员GT;
公共财产CustomerID作为字符串

端类




公共级客户

公共属性CustomerStatuses方式列表(顾客)

端类


I've managed to create a deserialization by creating a data contract class (after nagging on the customer a metric ton). My problem is that both the fields I've declared, are returning empty stuff. So, I've looked at it and realized that the JSON object is nested and I'm unclear on how to access the parts that are inside.

The data contract is like this but I'm getting Bopp as null (or empty string, not sure which) and Mopp as a bunch of zeros.

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

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

I thought that the data was on this form.

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

However, apparently, they moved the object inside an other so it's more like this.

[ "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"
  }
]]

How can I redesign the data contract to make it access the right fields? Or am I missing something and the name of the data member mustn't differ from the field (i.e. Beep and Bopp won't work)?!

EDIT:

As requested, I'm posting (almost) live data from the string.

{"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},
  ...

And here's the actual data contract.

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

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

解决方案

Your class structure needs to match the 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

这篇关于如何反序列化嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆