【问题标题】:Unhandled exception. Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: [. Path 'data', line 1, position 19未处理的异常。 Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:[。路径“数据”,第 1 行,位置 19
【发布时间】:2022-07-30 03:52:09
【问题描述】:

我正在开发一个天气应用程序,我需要从服务器获取天气数据。接收到的数据存储在响应变量中,在打印响应时我得到了正确的 Json 数据,但是在将其转换为字典并打印值时出现了一些错误,请帮助解决它。提前致谢

C# 代码:

using System;
using System.Net;
using Newtonsoft.Json;
class Program
{
static void Main(){
    var request = WebRequest.Create("website link");
    request.Credentials = CredentialCache.DefaultCredentials;
    var response = request.GetResponse();
    using (Stream dataStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            var myDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseFromServer);
            if (myDictionary != null)
            {
                foreach (var key in myDictionary.Keys) //Error is here at myDictionary.Keys
                { 
                    Console.WriteLine(myDictionary[key]); 
                } 
            }
        }
    }
}

错误:

    Unhandled exception. Newtonsoft.Json.JsonReaderException: Unexpected character 
    encountered while parsing value: [. Path 'data', line 1, position 19.
        at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
        at Newtonsoft.Json.JsonTextReader.ReadAsString()
        at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, String id)
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
        at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
        at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
        at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
        at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
        at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
        at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
        at Program.Main() in F:\Lenovo\Documents\web\C#\Program.cs:line 16

    

服务器响应为字符串:

已验证的 JSON 文件

    {
    "count": 1,
    "data": [{
        "rh": 70,
        "pod": "n",
        "lon": 80.27847,
        "pres": 1002.9,
        "timezone": "Asia\/Kolkata",
        "ob_time": "2022-03-22 12:30",
        "country_code": "IN",
        "clouds": 25,
        "ts": 1647952200,
        "solar_rad": 149.9,
        "state_code": "25",
        "city_name": "Chennai",
        "wind_spd": 4.1,
        "wind_cdir_full": "southeast",
        "wind_cdir": "SE",
        "slp": 1004,
        "vis": 5,
        "h_angle": 90,
        "sunset": "12:50",
        "dni": 515.54,
        "dewpt": 25.8,
        "snow": 0,
        "uv": 1.77082,
        "precip": 0,
        "wind_dir": 130,
        "sunrise": "00:41",
        "ghi": 151.03,
        "dhi": 57.44,
        "aqi": 156,
        "lat": 13.08784,
        "weather": {
            "icon": "c02n",
            "code": 801,
            "description": "Few clouds"
        },
        "datetime": "2022-03-22:12",
        "temp": 32,
        "station": "VOMM",
        "elev_angle": 11.39,
        "app_temp": 40.5
    }],
    "minutely": [{
        "timestamp_utc": "2022-03-22T12:57:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:27:00",
        "ts": 1647953820,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T12:58:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:28:00",
        "ts": 1647953880,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T12:59:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:29:00",
        "ts": 1647953940,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:00:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:30:00",
        "ts": 1647954000,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:01:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:31:00",
        "ts": 1647954060,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:02:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:32:00",
        "ts": 1647954120,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:03:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:33:00",
        "ts": 1647954180,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:04:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:34:00",
        "ts": 1647954240,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:05:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:35:00",
        "ts": 1647954300,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:06:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:36:00",
        "ts": 1647954360,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:07:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:37:00",
        "ts": 1647954420,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:08:00",
        "snow": 0,
        "temp": 29.1,
        "timestamp_local": "2022-03-22T18:38:00",
        "ts": 1647954480,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:09:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:39:00",
        "ts": 1647954540,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:10:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:40:00",
        "ts": 1647954600,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:11:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:41:00",
        "ts": 1647954660,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:12:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:42:00",
        "ts": 1647954720,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:13:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:43:00",
        "ts": 1647954780,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:14:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:44:00",
        "ts": 1647954840,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:15:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:45:00",
        "ts": 1647954900,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:16:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:46:00",
        "ts": 1647954960,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:17:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:47:00",
        "ts": 1647955020,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:18:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:48:00",
        "ts": 1647955080,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:19:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:49:00",
        "ts": 1647955140,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:20:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:50:00",
        "ts": 1647955200,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:21:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:51:00",
        "ts": 1647955260,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:22:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:52:00",
        "ts": 1647955320,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:23:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:53:00",
        "ts": 1647955380,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:24:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:54:00",
        "ts": 1647955440,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:25:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:55:00",
        "ts": 1647955500,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:26:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:56:00",
        "ts": 1647955560,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:27:00",
        "snow": 0,
        "temp": 29,
        "timestamp_local": "2022-03-22T18:57:00",
        "ts": 1647955620,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:28:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T18:58:00",
        "ts": 1647955680,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:29:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T18:59:00",
        "ts": 1647955740,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:30:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:00:00",
        "ts": 1647955800,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:31:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:01:00",
        "ts": 1647955860,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:32:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:02:00",
        "ts": 1647955920,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:33:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:03:00",
        "ts": 1647955980,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:34:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:04:00",
        "ts": 1647956040,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:35:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:05:00",
        "ts": 1647956100,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:36:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:06:00",
        "ts": 1647956160,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:37:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:07:00",
        "ts": 1647956220,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:38:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:08:00",
        "ts": 1647956280,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:39:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:09:00",
        "ts": 1647956340,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:40:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:10:00",
        "ts": 1647956400,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:41:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:11:00",
        "ts": 1647956460,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:42:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:12:00",
        "ts": 1647956520,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:43:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:13:00",
        "ts": 1647956580,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:44:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:14:00",
        "ts": 1647956640,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:45:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:15:00",
        "ts": 1647956700,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:46:00",
        "snow": 0,
        "temp": 28.9,
        "timestamp_local": "2022-03-22T19:16:00",
        "ts": 1647956760,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:47:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:17:00",
        "ts": 1647956820,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:48:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:18:00",
        "ts": 1647956880,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:49:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:19:00",
        "ts": 1647956940,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:50:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:20:00",
        "ts": 1647957000,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:51:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:21:00",
        "ts": 1647957060,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:52:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:22:00",
        "ts": 1647957120,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:53:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:23:00",
        "ts": 1647957180,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:54:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:24:00",
        "ts": 1647957240,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:55:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:25:00",
        "ts": 1647957300,
        "precip": 0
    }, {
        "timestamp_utc": "2022-03-22T13:56:00",
        "snow": 0,
        "temp": 28.8,
        "timestamp_local": "2022-03-22T19:26:00",
        "ts": 1647957360,
        "precip": 0
    }]
}

【问题讨论】:

  • 您正试图将那个复杂的对象反序列化为Dictionary&lt;string, string&gt;。在Dictionary&lt;string, string&gt; 中,所有值都是字符串。它们不能是数组、其他对象等。但是,"data" 键的值不是字符串——它是一个包含对象的数组。您需要一个比简单的Dictionary&lt;string, string&gt; 更复杂的模型。尝试将您的 JSON 粘贴到此网站中,这将生成一个合适的模型:json2csharp.com

标签: c# json .net json.net unhandled-exception


【解决方案1】:

我不知道你需要如何使用数据,但是你可以将它转换为字典

 var weather = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);

  // or just
var weather = JObject.Parse(json);

如何使用

    foreach (var prop in  ((JObject) ((JArray) weather["data"])[0]).Properties())
    {
        Console.WriteLine($"Name: {prop.Name}      Value:  {prop.Value} ");
    }
    
    //or minutely data
    
    foreach (var item in   ((JArray) weather["minutely"]))
    foreach (var prop in  ((JObject) item).Properties())
    {
        Console.WriteLine($"Name:   {prop.Name}         Value:    {prop.Value} ");
    }

【讨论】:

    【解决方案2】:

    您不能将Dictionary&lt;string,string&gt; 用于此数据。 Dictionary&lt;string,string&gt; 看起来像这样:

    {"field1": "value1", "field2": "value2"}
    

    我在VS2019中使用了Paste JSON As Classes功能,得到如下:

        public class Rootobject
        {
            public int count { get; set; }
            public Datum[] data { get; set; }
            public Minutely[] minutely { get; set; }
        }
    
        public class Datum
        {
            public int rh { get; set; }
            public string pod { get; set; }
            public float lon { get; set; }
            public float pres { get; set; }
            public string timezone { get; set; }
            public string ob_time { get; set; }
            public string country_code { get; set; }
            public int clouds { get; set; }
            public int ts { get; set; }
            public float solar_rad { get; set; }
            public string state_code { get; set; }
            public string city_name { get; set; }
            public float wind_spd { get; set; }
            public string wind_cdir_full { get; set; }
            public string wind_cdir { get; set; }
            public int slp { get; set; }
            public int vis { get; set; }
            public int h_angle { get; set; }
            public string sunset { get; set; }
            public float dni { get; set; }
            public float dewpt { get; set; }
            public int snow { get; set; }
            public float uv { get; set; }
            public int precip { get; set; }
            public int wind_dir { get; set; }
            public string sunrise { get; set; }
            public float ghi { get; set; }
            public float dhi { get; set; }
            public int aqi { get; set; }
            public float lat { get; set; }
            public Weather weather { get; set; }
            public string datetime { get; set; }
            public int temp { get; set; }
            public string station { get; set; }
            public float elev_angle { get; set; }
            public float app_temp { get; set; }
        }
    
        public class Weather
        {
            public string icon { get; set; }
            public int code { get; set; }
            public string description { get; set; }
        }
    
        public class Minutely
        {
            public DateTime timestamp_utc { get; set; }
            public int snow { get; set; }
            public float temp { get; set; }
            public DateTime timestamp_local { get; set; }
            public int ts { get; set; }
            public int precip { get; set; }
        }
    

    尝试插入这些类,然后将代码更改为JsonConvert.DeserializeObject&lt;RootObject&gt;

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 1970-01-01
      • 2020-01-15
      • 2016-11-13
      • 2020-11-26
      • 2018-07-25
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多