C#Newtonsoft错误

时间:2017-08-09 12:44:37

标签: c# json json.net

我试图在C#中解析一些JSON并且出现错误。

" text"下面的变量是有效的JSON,它具有"每日"字段,其中包含"数据" field,有一个数组值,第一个元素是我需要的对象。

我试图这样做:

dynamic blobtext = JObject.Parse(text);   //line 61
JObject weather = blobtext.daily.data[0]; //line 62

这引发了以下错误:

System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at Newtonsoft.Json.Linq.JToken.EnsureValue(JToken value)
   at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
   at DbMgr.DarkSky.<UpdateWeatherHistory>d__2.MoveNext() in C:\...\Program.cs:line 62

现在,我知道那条路上有一个有效的对象。有什么想法会发生什么?

编辑:JSON低于

{
    "latitude": 31.2291308312072,
    "longitude": 34.3353435935438,
    "timezone": "Africa/Cairo",
    "offset": 2,
    "currently": {
        "time": 1262390401,
        "summary": "Clear",
        "icon": "clear-night",
        "precipType": "rain",
        "temperature": 11.1,
        "apparentTemperature": 11.1,
        "dewPoint": 7.39,
        "humidity": 0.78,
        "windSpeed": 1.19,
        "windBearing": 141,
        "visibility": 8.66,
        "cloudCover": 0.1,
        "pressure": 1018.57,
        "uvIndex": 0
    },
    "hourly": {
        "summary": "Clear throughout the day.",
        "icon": "clear-day",
        "data": [{
            "time": 1262383200,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 10.82,
            "apparentTemperature": 10.82,
            "dewPoint": 8.31,
            "humidity": 0.84,
            "windSpeed": 1.99,
            "windBearing": 148,
            "visibility": 8.9,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262386800,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 10.54,
            "apparentTemperature": 10.54,
            "dewPoint": 8.49,
            "humidity": 0.87,
            "windSpeed": 2.09,
            "windBearing": 169,
            "visibility": 8.24,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262390400,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 11.1,
            "apparentTemperature": 11.1,
            "dewPoint": 7.39,
            "humidity": 0.78,
            "windSpeed": 1.19,
            "windBearing": 141,
            "visibility": 8.66,
            "cloudCover": 0.1,
            "pressure": 1018.57,
            "uvIndex": 0
        },
        {
            "time": 1262394000,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 10.06,
            "apparentTemperature": 10.06,
            "dewPoint": 7.29,
            "humidity": 0.83,
            "windSpeed": 1.81,
            "windBearing": 157,
            "visibility": 8.14,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262397600,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 10.37,
            "apparentTemperature": 10.37,
            "dewPoint": 7.24,
            "humidity": 0.81,
            "windSpeed": 2.07,
            "windBearing": 158,
            "visibility": 8.27,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262401200,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 10.07,
            "apparentTemperature": 10.07,
            "dewPoint": 7.31,
            "humidity": 0.83,
            "windSpeed": 1.34,
            "windBearing": 134,
            "visibility": 9,
            "cloudCover": 0,
            "pressure": 1018.52,
            "uvIndex": 0
        },
        {
            "time": 1262404800,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 10.01,
            "apparentTemperature": 10.01,
            "dewPoint": 6.43,
            "humidity": 0.78,
            "windSpeed": 3.46,
            "windBearing": 166,
            "visibility": 8.27,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262408400,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 10.92,
            "apparentTemperature": 10.92,
            "dewPoint": 6.67,
            "humidity": 0.75,
            "windSpeed": 1.52,
            "windBearing": 157,
            "visibility": 8.56,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262412000,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 11.97,
            "apparentTemperature": 11.97,
            "dewPoint": 6.78,
            "humidity": 0.71,
            "windSpeed": 1.15,
            "windBearing": 151,
            "visibility": 10.77,
            "cloudCover": 0.11,
            "pressure": 1018.68,
            "uvIndex": 1
        },
        {
            "time": 1262415600,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 16.46,
            "apparentTemperature": 16.46,
            "dewPoint": 7.92,
            "humidity": 0.57,
            "windSpeed": 3.02,
            "windBearing": 177,
            "visibility": 9.38,
            "cloudCover": 0,
            "uvIndex": 1
        },
        {
            "time": 1262419200,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 19.13,
            "apparentTemperature": 19.13,
            "dewPoint": 7.51,
            "humidity": 0.47,
            "windSpeed": 4.64,
            "windBearing": 162,
            "visibility": 9.48,
            "cloudCover": 0,
            "uvIndex": 2
        },
        {
            "time": 1262422800,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 22.06,
            "apparentTemperature": 22.06,
            "dewPoint": 6.72,
            "humidity": 0.37,
            "windSpeed": 2.53,
            "windBearing": 181,
            "visibility": 10.77,
            "cloudCover": 0.21,
            "pressure": 1017.43,
            "uvIndex": 3
        },
        {
            "time": 1262426400,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 23.49,
            "apparentTemperature": 23.49,
            "dewPoint": 5.71,
            "humidity": 0.32,
            "windSpeed": 3.69,
            "windBearing": 195,
            "visibility": 9.75,
            "cloudCover": 0,
            "uvIndex": 3
        },
        {
            "time": 1262430000,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 24.63,
            "apparentTemperature": 24.63,
            "dewPoint": 7.13,
            "humidity": 0.33,
            "windSpeed": 4.13,
            "windBearing": 195,
            "visibility": 9.75,
            "cloudCover": 0,
            "uvIndex": 3
        },
        {
            "time": 1262433600,
            "summary": "Partly Cloudy",
            "icon": "partly-cloudy-day",
            "precipType": "rain",
            "temperature": 25.28,
            "apparentTemperature": 25.28,
            "dewPoint": 4.48,
            "humidity": 0.26,
            "windSpeed": 3.21,
            "windBearing": 200,
            "visibility": 10.88,
            "cloudCover": 0.34,
            "pressure": 1014.82,
            "uvIndex": 1
        },
        {
            "time": 1262437200,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 24.79,
            "apparentTemperature": 24.79,
            "dewPoint": 4.9,
            "humidity": 0.28,
            "windSpeed": 2.95,
            "windBearing": 218,
            "visibility": 9.4,
            "cloudCover": 0,
            "uvIndex": 1
        },
        {
            "time": 1262440800,
            "summary": "Clear",
            "icon": "clear-day",
            "precipType": "rain",
            "temperature": 23.73,
            "apparentTemperature": 23.73,
            "dewPoint": 5.56,
            "humidity": 0.31,
            "windSpeed": 1.65,
            "windBearing": 235,
            "visibility": 9.4,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262444400,
            "summary": "Partly Cloudy",
            "icon": "partly-cloudy-night",
            "precipType": "rain",
            "temperature": 21.36,
            "apparentTemperature": 21.36,
            "dewPoint": 5.29,
            "humidity": 0.35,
            "windSpeed": 1.38,
            "windBearing": 216,
            "visibility": 10.91,
            "cloudCover": 0.38,
            "pressure": 1014.74,
            "uvIndex": 0
        },
        {
            "time": 1262448000,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 18.11,
            "apparentTemperature": 18.11,
            "dewPoint": 6.43,
            "humidity": 0.46,
            "windSpeed": 1.01,
            "windBearing": 171,
            "visibility": 9.59,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262451600,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 17.63,
            "apparentTemperature": 17.63,
            "dewPoint": 6.84,
            "humidity": 0.49,
            "windSpeed": 0.93,
            "windBearing": 147,
            "visibility": 9.59,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262455200,
            "summary": "Partly Cloudy",
            "icon": "partly-cloudy-night",
            "precipType": "rain",
            "temperature": 15.95,
            "apparentTemperature": 15.95,
            "dewPoint": 6.43,
            "humidity": 0.53,
            "windSpeed": 0.9,
            "windBearing": 174,
            "visibility": 10.12,
            "cloudCover": 0.38,
            "pressure": 1015.66,
            "uvIndex": 0
        },
        {
            "time": 1262458800,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 14.79,
            "apparentTemperature": 14.79,
            "dewPoint": 6.27,
            "humidity": 0.57,
            "windSpeed": 1.69,
            "windBearing": 164,
            "visibility": 9.48,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262462400,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 14.67,
            "apparentTemperature": 14.67,
            "dewPoint": 6.08,
            "humidity": 0.56,
            "windSpeed": 1.54,
            "windBearing": 311,
            "visibility": 9.48,
            "cloudCover": 0,
            "uvIndex": 0
        },
        {
            "time": 1262466000,
            "summary": "Clear",
            "icon": "clear-night",
            "precipType": "rain",
            "temperature": 13.12,
            "apparentTemperature": 13.12,
            "dewPoint": 4.67,
            "humidity": 0.57,
            "windSpeed": 0.83,
            "windBearing": 184,
            "visibility": 10.28,
            "cloudCover": 0.19,
            "pressure": 1016.1,
            "uvIndex": 0
        }]
    },
    "daily": {
        "data": [{
            "time": 1262383200,
            "summary": "Clear throughout the day.",
            "icon": "clear-day",
            "sunriseTime": 1262407368,
            "sunsetTime": 1262443961,
            "moonPhase": 0.56,
            "precipType": "rain",
            "temperatureMin": 10.01,
            "temperatureMinTime": 1262404800,
            "temperatureMax": 25.28,
            "temperatureMaxTime": 1262433600,
            "apparentTemperatureMin": 10.01,
            "apparentTemperatureMinTime": 1262404800,
            "apparentTemperatureMax": 25.28,
            "apparentTemperatureMaxTime": 1262433600,
            "dewPoint": 6.58,
            "humidity": 0.57,
            "windSpeed": 1.83,
            "windBearing": 179,
            "visibility": 9.46,
            "cloudCover": 0.07,
            "pressure": 1016.81,
            "uvIndex": 3,
            "uvIndexTime": 1262422800
        }]
    },
    "flags": {
        "sources": ["isd"],
        "isd-stations": ["401762-99999",
        "401790-99999",
        "401800-99999",
        "401830-99999",
        "401900-99999",
        "401980-99999",
        "402400-99999",
        "402700-99999",
        "402720-99999",
        "402960-99999",
        "403100-99999",
        "623350-99999",
        "623360-99999",
        "623370-99999",
        "624520-99999",
        "624530-99999"],
        "units": "si"
    }
}

1 个答案:

答案 0 :(得分:1)

使用var weather = blobtext.daily.data[0];代替JObject weather = blobtext.daily.data[0];

实际上blobtext.daily.data[0];不返回JObject,而是返回一些未知类型。所以你不能将它分配给JObject。

如果你问为什么没有编译时错误,我可以说是因为动态变量。 :)