使用嵌套 json 文件 visual basic

Working with nested json file visual basic

我有一个我想要反序列化的嵌套 json 文件,但我一直收到错误消息“对象引用未设置为对象的实例。”

这是我的 json 文件

[
{
    "Name": "Junius",
    "LastName": "Lekgwara",
    "CellNo": [
        {
            "CellC": "072685345",
            "Voda": "0728589303"
        }
    ]
},
{
    "Name": "Phuti",
    "LastName": "Gravel",
    "CellNo": [
        {
            "CellC": "08377777777",
            "Voda": "089888888888"
        }
    ]
},
{
    "Name": "Donald",
    "LastName": "Gravel",
    "CellNo": [
        {
            "CellC": "0791408989",
            "Voda": "0117689009"
        }
    ]
  }
]

这是我的人class

Public Class Person
Private _Name As String
Public Property Name() As String
    Get
        Return _Name
    End Get
    Set(ByVal value As String)
        _Name = value
    End Set
End Property
Private _LastName As String
Public Property LastName() As String
    Get
        Return _LastName
    End Get
    Set(ByVal value As String)
        _LastName = value
    End Set
End Property
Public Property cellno As CellNo
End Class

Public Class CellNo
Public Property CellC As String
Public Property Voda As String

End Class

人物列表class

Public Class PersonList
Private _ListPerson As List(Of Person)
Public Property ListPerson() As List(Of Person)
    Get
        Return _ListPerson
    End Get
    Set(ByVal value As List(Of Person))
        _ListPerson = value
    End Set
End Property
Sub New()
    _ListPerson = New List(Of Person)
End Sub
End Class

正在读取 json 文件,调用此子程序时出现错误

 Public Async Sub LoadFromFile()
    Dim reader As String = Await GetFile()
    Dim serializer As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(ObservableCollection(Of Person)))
    Dim ms As MemoryStream = New MemoryStream(Encoding.UTF8.GetBytes(reader))
    Dim listOf As ObservableCollection(Of Person) = New ObservableCollection(Of Person)
    listOf = serializer.ReadObject(ms)
    For Each item In listOf
        list.ListPerson.Add(item)
        Debug.WriteLine(item.Name + " " + item.LastName + " " + item.cellno.CellC + " " + item.cellno.Voda) 'Object reference not set to an instance of an object, on item.cellno.CellC and item.cellno.Voda
    Next
End Sub

我希望能够像

一样引用它
Dim person As Person = New Person()
lblName.Text = person.Name
lblLastName.Text = person.LastName
lblCellCNo.Text= person.cellno.CellC
lblCellVodaNo.Text= person.cellno.Voda

我该如何解决这个问题?

看..你的问题是
项目列表没有为你想要的准备好。

 item.cellno HAS CHILD !!! 


 item.cellno.CellC 
 item.cellno.Voda

必须迭代..

 For Each item In listOf
    list.ListPerson.Add(item)
    'Debug.WriteLine(item.Name + " " + item.LastName + " " + _ 

   'you know that you have just 2 items in : CellNo, and play with that.
   For i= 0 to 1
      Item.CellNo(i) ' do something with ..
   End For

Next

另请阅读:vb.net datatable Serialize to json

显示的 JSON 与您要使用的 class 结构有点不同。 Person 是已经在 JSON 中的列表或数组。 CellNo 也是一个数组,即使它出现在 that 样本中每个人只有一个 class 对象。

因此,您可以完全跳过 PersonList

Public Class CellNo
    Public Property CellC As String
    Public Property Voda As String
End Class

Public Class Person  
    Public Property Name As String
    Public Property LastName As String
    Public Property CellNo As List(Of CellNo)
End Class

反序列化为 List(Of Person):

Dim jstr = ... (wherever it comes from)

Dim jp As List(Of Person)
jp = JsonConvert.DeserializeObject(Of List(Of Person))(jstr)

现在无需进一步操作即可完全使用:

For Each P As Person In jp
    Console.WriteLine("{0}  Cell Voda:{1}", P.Name, P.CellNo(0).Voda)
Next

Junius Cell Voda: 0728589303
Phuti Cell Voda: 089888888888
Donald Cell Voda: 0117689009

可选地,您可以反序列化为一个 Person 数组,并将 Person.CellNo 也定义为一个数组。


i'm not using newtonsoft

使用 NET 得到相同的结果:

Imports System.Web.Script.Serialization

Dim jss As New JavaScriptSerializer
Dim jp As List(Of Person)

jp = jss.Deserialize(Of List(Of Person))(jstr)

使用CellNo作为数组解决了问题

 `"CellNo":[
             "072685345",
             "0728589303"
           ]

   For Each item In listOf
        list.ListPerson.Add(item)
        Debug.WriteLine(item.Name + " " + item.LastName)
        For Each i In item.CellNo
            Debug.WriteLine(i)
        Next
    Next

显示

Junius Lekgwara
Cell No: 072685345
Cell No: 0728589303

Phuti Gravel
Cell No: 08377777777
Cell No: 089888888888

Donald Gravel
Cell No: 0791408989
Cell No: 0117689009