从 JSON 响应中提取数据到 VB.Net 中的列表或数组

Extracting data from JSON response to list or array in VB.Net

我不太了解 JSON 的构成,所以我正在努力调整其他答案以满足我的需要。我真的希望有人能帮忙?具体来说,我想从 VB 中的以下 JSON 响应中获取 "elevations" 的列表或数组。我已经设法将响应转换为字符串并对其进行了解析,但这是我所能得到的。

VB.Net:

Imports Newtonsoft.Json.Linq

Dim urlstring As String = urlstring1 & coordstring & urlstring2
Dim srequest As HttpWebRequest = DirectCast(WebRequest.Create(urlstring), HttpWebRequest)
Dim responsestring As String
        'Execute http enquiry
        Try
            Dim sresponse As New StreamReader(srequest.GetResponse().GetResponseStream())
            responsestring = sresponse.ReadToEnd()
            sresponse.Close()
            'MessageBox.Show(responsestring)
        Catch ex As Exception
            MessageBox.Show("Error getting elevations, check internet connection")
            GoTo X
        End Try

Dim ser As JObject = JObject.Parse(responsestring)

JSON:

{  
   "authenticationResultCode":"ValidCredentials",  
   "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",  
   "copyright":"Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",  
   "resourceSets":[  
      {  
         "estimatedTotal":1,  
         "resources":[  
            {  
               "__type":"ElevationData:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",  
               "elevations":[1776,1775,1777,1776],  
               "zoomLevel":14  
            }  
         ]  
      }  
   ],  
   "statusCode":200,  
   "statusDescription":"OK",  
   "traceId":"8d57dbeb0bb94e7ca67fd25b4114f5c3"  
}

使用一些东西来帮助你形象化,比如 https://jsonformatter.curiousconcept.com/ 折叠所有然后在展开时写下你的路径。

当您到达 {} 时键入名称,当您到达 [] 时键入数字。

YourArray = ser("resourceSets")(0)("resources")(0)("elevations")

将 CruleD 的响应添加到我的代码中:

Imports Newtonsoft.Json.Linq

Dim urlstring As String = urlstring1 & coordstring & urlstring2
Dim srequest As HttpWebRequest = DirectCast(WebRequest.Create(urlstring), HttpWebRequest)
Dim responsestring As String
'Execute http enquiry
Try
            Dim sresponse As New StreamReader(srequest.GetResponse().GetResponseStream())
            responsestring = sresponse.ReadToEnd()
            sresponse.Close()

Catch ex As Exception
            MessageBox.Show("Error getting elevations, check internet connection")
            GoTo X
End Try

Dim ser As JObject = JObject.Parse(responsestring)
For Each token As JToken In ser("resourceSets")(0)("resources")(0)("elevations")
            'Do something here with each item in the list such as add to an array
            'e.g. elevationarray(tokennumber) = token
            'tokennumber = tokennumber + 1
Next