【问题标题】:How to access data from objects nested in json array如何访问嵌套在 json 数组中的对象的数据
【发布时间】:2017-12-26 12:38:41
【问题描述】:

我需要使用 vb.net 4.0 中的 javascriptserializer 访问嵌套的 json 对象数组中的数据,以便能够进一步操作它。

这是示例字符串:

json: {"multicast_id":216,"success":3,"failure":3,"canonical_ids":1,"results":[{"message_id":"1:0408"},{"errord":"Unavailable"}]}

这是我目前编写的代码:

Class main
Public Property multicast_id As Integer
Public Property success As Integer
Public Property failure As Integer
Public Property canonical_ids As Integer
Public Property results As Results()
End Class

Class Results
Public Property message_id As String
Public Property errord As String
End Class


Public Class Form1

Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
    Dim json As String
    json = txtInsert.Text

    Try
        Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
        Dim exmp As main = New main
        exmp = ser.Deserialize(Of main)(json)

        Console.WriteLine(exmp.canonical_ids)
        Console.WriteLine(exmp.multicast_id)

        For Each item As Object In exmp.results
            For Each example As String In item
                Console.WriteLine("example")
            Next
        Next

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
End Class

每个语句的内部都出现错误。如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

标签: arrays json vb.net javascriptserializer


【解决方案1】:

您正在正确获取嵌套对象。

相反,您的问题是,一旦获得该嵌套对象,您就会尝试使用For Each 来获取该结果的每个字符串属性。那是无效的。

例如,这是行不通的,因为Results 没有实现 IEnumerable 所以你不能使用For Each on in:

Dim r as new Results();
r.message_id = "1:0408"
r.errord = "Unavailable"
For Each example As String In r ' <- this will throw an error
Next

一旦你引用了Results,你就需要通过属性名来访问它:

For Each item As Results In exmp.results
    Console.WriteLine("message_id: {0}", item.message_id)
    Console.WriteLine("errord: {0}", item.errord)
Next

如果您需要按名称访问属性,请将结果更改为其他类型。

例如,您可以将结果定义为Dictionary(of String, Object) 的数组,如下所示:

Class main
    Public Property multicast_id As Integer
    Public Property success As Integer
    Public Property failure As Integer
    Public Property canonical_ids As Integer
    Public Property results As Dictionary(Of String, Object)()
End Class

那么你就可以使用了:

For Each item As Object In exmp.results 'item will be a Dictionary(Of String, Object)
    For Each example As Object In item ' example will be a KeyValuePari(Of String, Object)
        Console.WriteLine(example.key)
        Console.WriteLine(example.value)
    Next
Next

或者这样的名字:

    For Each item As Object In exmp.results 'item will be a Dictionary(Of String, Object)
        Console.WriteLine(item("message_id"))
        Console.WriteLine(item("errord"))
    Next
Next

【讨论】:

    【解决方案2】:

    你不需要那个内在的 For Each。您已经使用第一个循环在阵列中旋转。您也可以更具体地使用循环中的类型:

        For Each item As Results In exmp.results
            Console.WriteLine(item.message_id & " : " & item.errord)
        Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-13
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多