无法访问第二级JSON字符串

时间:2019-01-02 07:11:13

标签: arrays json vb.net parsing

对于一个编程学校项目,我正在制作一个气象应用程序,该应用程序需要显示当前温度。在JSON字符串中,当前温度处于第二级,我不知道如何访问。将来,如果您有任何想法,我也希望访问此字符串中包含的每小时和每天的预测。任何帮助,将不胜感激。这是我的数据:

{
    "latitude":-    32.9283,
    "longitude":151.7817,
    "timezone":"Australia/Sydney",
    "currently":{
        "time":1546405401,
        "summary":"Clear",
        "icon":"clear-day",
        "precipIntensity":0,
        "precipProbability":0,
        "temperature":93.03,
        "apparentTemperature":93.03,
        "dewPoint":58.07,
        "humidity":0.31,
        "pressure":1009.17,
        "windSpeed":14.77,
        "windGust":19.66,
        "windBearing":68,
        "cloudCover":0,
        "uvIndex":7,
        "visibility":7.75,
        "ozone":276.4
    },
    "hourly":{
        "summary":"Clear throughout the day.",
        "icon":"clear-day",
        "data":[
            {
                "time":1546405200,
                "summary":"Clear",
                "icon":"clear-day",
                "precipIntensity":0,
                "precipProbability":0,
                "temperature":93.25,
                "apparentTemperature":93.25,
                "dewPoint":57.89,
                "humidity":0.31,
                "pressure":1009.18,
                "windSpeed":14.78,
                "windGust":19.53,
                "windBearing":68,
                "cloudCover":0,
                "uvIndex":7,
                "visibility":7.58,
                "ozone":276.43
            },
            {
                "time":1546408800,
                "summary":"Clear",
                "icon":"clear-day",
                "precipIntensity":0,
                "precipProbability":0,
                "temperature":89.44,
                "apparentTemperature":89.44,
                "dewPoint":60.5,
                "humidity":0.38,
                "pressure":1008.99,
                "windSpeed":14.58,
                "windGust":21.84,
                "windBearing":66,
                "cloudCover":0,
                "uvIndex":4,
                "visibility":10,
                "ozone":275.8
            },
            ...

这是我的代码:

Imports System.IO
Imports System.Net
Imports System.Drawing
Imports System.Web.Script.Serialization
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader

    request = DirectCast(WebRequest.Create("https://api.darksky.net/forecast/412498ac9648999c8185723817a897d3/-32.9283,151.7817"), HttpWebRequest)
    response = DirectCast(request.GetResponse(), HttpWebResponse)
    reader = New StreamReader(response.GetResponseStream())

    Dim data As String
    data = reader.ReadToEnd()

    Dim jsonObject As JObject = JObject.Parse(data)
    MessageBox.Show(jsonObject.SelectToken("currently").ToString)
    Dim JsonArray As JArray = JArray.Parse(jsonObject.SelectToken("currently").ToString)
    MessageBox.Show(jsonObject.SelectToken("temperature").ToString)

错误消息:

  

Newtonsoft.Json.JsonReaderException:'从JsonReader读取JArray时出错。当前JsonReader项不是数组:StartObject。路径”,第1行,位置1。'

1 个答案:

答案 0 :(得分:0)

就像错误指出[]不是数组,它是一个对象一样。数组用方括号{}表示,而对象使用花括号Dim currentlyObject As JObject = DirectCast(jsonObject("currently"), JObject) 'Example usage. MessageBox.Show(currentlyObject("temperature"))

尝试以下方法:

Alter database old_name {MODIFY NAME = new_name}