I am working on a weather application for that I need to get the weather data from the server. the received data is stored in response variable, while printing the response I got the correct Json data, but got some error while converting it into dictionary and printing the values please help to solve it. Thanks In Advance
C# CODE:
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]);
}
}
}
}
}
ERROR:
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
RESPONSE FROM SERVER AS STRING:
VALIDATED JSON FILE
{
"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
}]
}
CodePudding user response:
You can't use Dictionary<string,string>
with this data. Dictionary<string,string>
would look like this:
{"field1": "value1", "field2": "value2"}
I used the Paste JSON As Classes feature in VS2019 and got the following:
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; }
}
Try inserting these classes and then change code to JsonConvert.DeserializeObject<RootObject>
CodePudding user response:
I don't know how do you need to use data, but you can convert it to dictionary< string, object >
var weather = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);
// or just
var weather = JObject.Parse(json);
how to use
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} ");
}