Asp.net 将json作业对象数据插入标签和文本框

Asp.net 将json作业对象数据插入标签和文本框,asp.net,json,vb.net,Asp.net,Json,Vb.net,我的问题如下: 我创建了一个公共子系统,目前看起来如下所示: Public Sub eniroContacts() Dim phone As String = newCustPhone1.text Dim url As String = "http://bedrift.telefonkatalogen.no/tk/search.php?qry=" + phone + "&from=1&to=27&format=json&userna

我的问题如下:

我创建了一个公共子系统,目前看起来如下所示:

Public Sub eniroContacts()
        Dim phone As String = newCustPhone1.text
        Dim url As String = "http://bedrift.telefonkatalogen.no/tk/search.php?qry=" + phone + "&from=1&to=27&format=json&username=user&password=pw"
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim o As JObject = JObject.Parse(reader.ReadToEnd)

        lblMsg.Text = o.ToString

        reader.Close()
        response.Close()
    End Sub
到目前为止还不错,我的lblMsg.Text显示了我想要从中获取数据的json字符串(格式清晰):

我现在想做的是实际获取字符串中的一些值,并将它们放入我的一些文本框和标签中,但我不知道如何才能做到这一点。例如,我想要值“fornavn”、“tlfnr”和“fylke”。本例中的值是Martin、99887766和Oslo,它们应该放在文本框1、2和3中

此外,我还想补充一点,有些字段可能不在搜索的字段中,比如名字(“在我的示例中为fornavn”)。当这不存在时,我试着写:

 Dim fornavn = o("result")("1")("listing")("duplicates")(0)("fornavn")
它崩溃了。在这些情况下,我可以做些什么来防止这种情况


非常感谢您提供任何线索和提示!:)

最简单的方法是直接使用属性名和数组索引(从)。大概是这样的:

Dim fornavn = o("result")("1")("listing")("duplicates")(0)("fornavn")
看起来响应中的结果和重复项的数量在不同的响应之间会发生变化,因此您可能无法对其进行硬编码(例如,您可能需要首先检查结果是否至少有一项,以及重复项是否至少有一项)

另一种方法是将字符串反序列化为强类型的.Net对象。您将创建镜像json结构的.Net类,并告诉json.Net将json字符串转换为这些类的.Net对象实例。看。这需要做更多的工作,因为您需要创建.Net类,并且需要了解反序列化过程。如果您想探索此选项,请查看和提出问题

编辑 给定json结构,在json对象的
result
属性中手动获取类似“1”的属性可能更容易。 然后,您可以选择手动继续钻取对象并迭代重复项,应用所需的任何逻辑,或者反序列化为.Net对象

以下代码将获得
结果
属性,使用键1、2等迭代其所有属性,并使用这两种方法在每个副本中打印名称:

Sub Main()
    Dim JsonStr = "... the json string ..."     

    Dim o As JObject = JObject.Parse(JsonStr)
    Dim results = o("result")
    For Each resultProperty In results.Value(Of JObject)()
        'Only get properties like "1" inside the root "result" property
        If Not Integer.TryParse(resultProperty.Key, Nothing) Then Continue For

        'Approach 1: Manually Iterate over the duplicates array inside each result
        Dim duplicatesArray = resultProperty.Value("listing")("duplicates").Value(Of JArray)()
        For Each duplicate In duplicatesArray
            'Make sure there is a fornavn property
            If duplicate("fornavn") Is Nothing Then Continue For
            Console.WriteLine(duplicate("fornavn"))
        Next

        'Approach 2: Deserialize the listing into a .Net object
        Dim serializer As JsonSerializer = New JsonSerializer()
        Dim resultObject As Result = JsonConvert.DeserializeObject(Of Result)(resultProperty.Value.ToString())
        For Each duplicateObject In resultObject.listing.duplicates
            Console.WriteLine(duplicateObject.fornavn)
        Next
    Next

    Console.ReadKey()
End Sub

Class Result
    Property listing As Listing
End Class

Class Listing
    Property table As String
    Property id As String
    Property duplicates As Duplicate()
End Class

Class Duplicate
    Property table As String
    Property id As String
    Property idlinje As String
    Property fornavn As String
    'Continues with all the other properties...
End Class

请注意,我刚刚快速编写了这段代码,因此如果json响应中不总是存在某些属性,您可能需要对其进行调整。

您说的结果和重复项可以超过1次,这是正确的。它实际上可以是0到10个以上。我用第一种方法得到了结果,但需要验证是否有比1更多的结果和重复。最简单的方法是什么?我也试着去研究反序列化,但是现在我觉得它很混乱。我创建了这些类,但不知道如何使用它们。如果需要,我可以添加它们,但是创建它们时得到的字符串非常长。非常感谢您为我编写所有代码。我已经看过了,并将尝试从方法2开始。我看到代码本身工作得非常好!:)再次感谢您在这件事上花时间帮助我!
Sub Main()
    Dim JsonStr = "... the json string ..."     

    Dim o As JObject = JObject.Parse(JsonStr)
    Dim results = o("result")
    For Each resultProperty In results.Value(Of JObject)()
        'Only get properties like "1" inside the root "result" property
        If Not Integer.TryParse(resultProperty.Key, Nothing) Then Continue For

        'Approach 1: Manually Iterate over the duplicates array inside each result
        Dim duplicatesArray = resultProperty.Value("listing")("duplicates").Value(Of JArray)()
        For Each duplicate In duplicatesArray
            'Make sure there is a fornavn property
            If duplicate("fornavn") Is Nothing Then Continue For
            Console.WriteLine(duplicate("fornavn"))
        Next

        'Approach 2: Deserialize the listing into a .Net object
        Dim serializer As JsonSerializer = New JsonSerializer()
        Dim resultObject As Result = JsonConvert.DeserializeObject(Of Result)(resultProperty.Value.ToString())
        For Each duplicateObject In resultObject.listing.duplicates
            Console.WriteLine(duplicateObject.fornavn)
        Next
    Next

    Console.ReadKey()
End Sub

Class Result
    Property listing As Listing
End Class

Class Listing
    Property table As String
    Property id As String
    Property duplicates As Duplicate()
End Class

Class Duplicate
    Property table As String
    Property id As String
    Property idlinje As String
    Property fornavn As String
    'Continues with all the other properties...
End Class