I have successfully parsed Openweather's 'Current Weather Data' using Json, but I'm having a problem with 'One Call'. It returns the data arranged a little differently. I can pull out everything except the 'weather' list. The class I created for it returns null values.
{
"lat": 41.9585,
"lon": -78.1892,
"timezone": "America/New_York",
"timezone_offset": -14400,
"current": {
"dt": 1652559634,
"sunrise": 1652522008,
"sunset": 1652574293,
"temp": 74.53,
"feels_like": 75.11,
"pressure": 1014,
"humidity": 73,
"dew_point": 65.32,
"uvi": 2.37,
"clouds": 100,
"visibility": 10000,
"wind_speed": 7.47,
"wind_deg": 173,
"wind_gust": 10.63,
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
]
}
}
string url = string.Format("https://api.openweathermap.org/data/2.5/onecall?lat={0}&lon={1}&exclude=minutely,hourly,daily&appid={2}&units=imperial", 41.958496166, -78.18916591, APIKey);
string jason = web.DownloadString(url);
WeatherInfo.root info = JsonConvert.DeserializeObject<WeatherInfo.root>(jason);
public static class WeatherInfo
{
public class current
{
public double dt { get; set; }
public double sunrise { get; set; }
public double sunset { get; set; }
public double temp { get; set; }
public double pressure { get; set; }
public double humidity { get; set; }
public double dew_point { get; set; }
public double uvi { get; set; }
public double clouds { get; set; }
public double visibility { get; set; }
public double wind_speed { get; set; }
public double wind_deg { get; set; }
public double wind_gust { get; set; }
}
public class weather
{
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class root
{
public current current { get; set; }
public List<weather> weather { get; set; }
}
}
Related
I have json string like this:
{
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
...
"quote": {
"USD": {
"price": 9283.92,
"volume_24h": 7155680000,
"percent_change_1h": -0.152774,
"percent_change_24h": 0.518894,
"market_cap": 158055024432,
"last_updated": "2018-08-09T22:53:32.000Z"
},
"BTC": {
"price": 1,
"volume_24h": 772012,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0,
"market_cap": 17024600,
"last_updated": "2018-08-09T22:53:32.000Z"
}
}
},
// objects like previous from which i need the data
],
"status": {
"timestamp": "2018-06-02T22:51:28.209Z",
...
}
}
How do I deserialize it into models like this:
public class MyModel
{
public string Name { get; set; }
public string Symbol { get; set; }
public string Price { get; set; }
public double Percent_change_1h { get; set; }
public double Percent_change_24h { get; set; }
public long Market_cap { get; set; }
public DateTime Last_updated { get; set; }
}
The field names in the model are the same as the key names in json string.
I'm new to C# and I couldn't find any helpful information about my question, especially because of this specific json string structure.
I'll be glad if you direct me any good links about this.
The model seems to be something like this.
public class Model
{
public List<Datum> data { get; set; }
public Status status { get; set; }
}
public class Status
{
public DateTime timestamp { get; set; }
}
public class Datum
{
public int id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public Quote quote { get; set; }
}
public class Quote
{
public USD USD { get; set; }
public BTC BTC { get; set; }
}
public class BTC
{
public int price { get; set; }
public int volume_24h { get; set; }
public int percent_change_1h { get; set; }
public int percent_change_24h { get; set; }
public int percent_change_7d { get; set; }
public int market_cap { get; set; }
public DateTime last_updated { get; set; }
}
public class USD
{
public double price { get; set; }
public object volume_24h { get; set; }
public double percent_change_1h { get; set; }
public double percent_change_24h { get; set; }
public object market_cap { get; set; }
public DateTime last_updated { get; set; }
}
You can also try creating model on (http://json2csharp.com/) by copying your valid json string.
Please let me know if this helps
Bottom line: You can (manually), but that's probably not what you're looking for.
Reason: Your model doesn't match the JSON structure, hence "manual"
You can use readily available tools in either Visual Studio or VS Code to help you with creating the proper model (e.g. Paste JSON As Code)
Once you get the "proper" model/s ready, go over JSON documentation for (de)serializing.
I had to fix some syntax errors on your json, so fixed version is following:
{
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"quote": {
"USD": {
"price": 9283.92,
"volume_24h": 7155680000,
"percent_change_1h": -0.152774,
"percent_change_24h": 0.518894,
"market_cap": 158055024432,
"last_updated": "2018-08-09T22:53:32.000Z"
},
"BTC": {
"price": 1,
"volume_24h": 772012,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0,
"market_cap": 17024600,
"last_updated": "2018-08-09T22:53:32.000Z"
}
}
}
],
"status": {
"timestamp": "2018-06-02T22:51:28.209Z"
}
}
Here is C# model classes matching with previous json:
public class Rootobject
{
public Datum[] data { get; set; }
public Status status { get; set; }
}
public class Status
{
public DateTime timestamp { get; set; }
}
public class Datum
{
public int id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public Quote quote { get; set; }
}
public class Quote
{
public USD USD { get; set; }
public BTC BTC { get; set; }
}
public class USD
{
public float price { get; set; }
public long volume_24h { get; set; }
public float percent_change_1h { get; set; }
public float percent_change_24h { get; set; }
public long market_cap { get; set; }
public DateTime last_updated { get; set; }
}
public class BTC
{
public int price { get; set; }
public int volume_24h { get; set; }
public int percent_change_1h { get; set; }
public int percent_change_24h { get; set; }
public int percent_change_7d { get; set; }
public int market_cap { get; set; }
public DateTime last_updated { get; set; }
}
Here is code snippet which you can use when deserializing your json. This snippet uses Json.NET-library.
var obj = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("object.json"));
I imagine this is more of a problem with my code rather than a problem with JSON.NET, but I'm not sure where I'm going wrong.
I have the following class below which is being used to serialise the json data found at this link: https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
To construct this class I used a JSON to C# generator found on google.
Also below are the methods I use to pull and serialise the JSON data into a WeatherData object.
private string GetJsonFromWeb(string resource, string city)
{
var request = new RestRequest(resource, Method.GET);
request.AddParameter("q", city);
request.AddParameter("APPID", "af5e6fd579e0ddb303afc1774487c77b");
var fullUrl = client.BuildUri(request);
Console.WriteLine("Full URL: " + fullUrl.AbsoluteUri);
var response = client.Execute(request);
string json = response.Content;
return json;
}
private WeatherData SerializeJsonToWeatherData(string json)
{
WeatherData weatherData = JsonConvert.DeserializeObject<WeatherData>(json);
return weatherData;
}
These methods work successfully, as the JSON data can be used later in the application to display information within the GUI. However the MainData part of the JSON data is set to null after the JSON is pulled from the API.
class WeatherData
{
public Coord Coord { get; set; }
public List<Weather> Weather { get; set; }
public string Base { get; set; }
public MainData MainData { get; set; }
public int Visibility { get; set; }
public Wind Wind { get; set; }
public Clouds Clouds { get; set; }
public int Dt { get; set; }
public Sys Sys { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public int Cod { get; set; }
}
public class Coord
{
public double Lon { get; set; }
public double Lat { get; set; }
}
public class Weather
{
public int Id { get; set; }
public string Main { get; set; }
public string Description { get; set; }
public string Icon { get; set; }
}
public class MainData
{
public double Temp { get; set; }
public int Pressure { get; set; }
public int Humidity { get; set; }
public double Temp_min { get; set; }
public double Temp_max { get; set; }
}
public class Wind
{
public double Speed { get; set; }
public int Deg { get; set; }
}
public class Clouds
{
public int All { get; set; }
}
public class Sys
{
public int Type { get; set; }
public int Id { get; set; }
public double Message { get; set; }
public string Country { get; set; }
public int Sunrise { get; set; }
public int Sunset { get; set; }
}
Sample JSON from above API
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 300,
"main": "Drizzle",
"description": "light intensity drizzle",
"icon": "09d"
}
],
"base": "stations",
"main": {
"temp": 280.32,
"pressure": 1012,
"humidity": 81,
"temp_min": 279.15,
"temp_max": 281.15
},
"visibility": 10000,
"wind": {
"speed": 4.1,
"deg": 80
},
"clouds": {
"all": 90
},
"dt": 1485789600,
"sys": {
"type": 1,
"id": 5091,
"message": 0.0103,
"country": "GB",
"sunrise": 1485762037,
"sunset": 1485794875
},
"id": 2643743,
"name": "London",
"cod": 200
}
The JSON property is called main not mainData. JSON.Net can't find a property called mainData ans thus does not populate the MainData property of your POCOs
This json is from a HTTPresponse from google API:
{
"results": [
{
"address_components": [
{
"long_name": "Europe",
"short_name": "Europe",
"types": [
"continent",
"establishment",
"natural_feature"
]
}
],
"formatted_address": "Europe",
"geometry": {
"bounds": {
"northeast": {
"lat": 82.1673907,
"lng": 74.3555001
},
"southwest": {
"lat": 34.5428,
"lng": -31.4647999
}
},
"location": {
"lat": 54.5259614,
"lng": 15.2551187
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 65,
"lng": 55
},
"southwest": {
"lat": 34,
"lng": -11
}
}
},
"place_id": "ChIJhdqtz4aI7UYRefD8s-aZ73I",
"types": [
"continent",
"establishment",
"natural_feature"
]
}
],
"status": "OK"
}
I built a list class:
public class geocode {
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast2
{
public int lat { get; set; }
public int lng { get; set; }
}
public class Southwest2
{
public int lat { get; set; }
public int lng { get; set; }
}
public class Viewport
{
public Northeast2 northeast { get; set; }
public Southwest2 southwest { get; set; }
}
public class Geometry
{
public Bounds bounds { get; set; }
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
}
And this is far I get:
nombrePapa = nombrePapa.ToUpper();
var resultadoGeocoding = ggo.geocoding(nombrePapa);
//var result = JsonConvert.DeserializeObject<listas.geocode.Result>(resultadoGeocoding);
List<listas.geocode> lista = JsonConvert.DeserializeObject<List<listas.geocode>>(resultadoGeocoding);
But this deserialization stuck me on:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Models.listas+geocode]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
I'm missing something?
What the error is telling you is that in order to correctly deserialize your JSON into a List<listas.geocode>, you'd need your JSON to represent an array. What you are giving the deserializer instead is an object, with two properties, results and status.
I'm guessing you used json2csharp to get your classes, since you already have a class for the root object. You just forgot to use it
To deserialize the response you need to do:
var response = JsonConvert.DeserializeObject<RootObject>(resultadoGeocoding);
var lista = response.Results;
Additional note: For status, you can use a string (easiest option), or you can use an enum as detailed here.
Here is the JSON string that needs to be converted:
{
"b2b": [
{
"ctin": "37ABCDE9552F3Z4",
"inv": [
{
"inum": "S008400",
"idt": "09-04-2016",
"val": 861786.91,
"pos": "6",
"rchrg": "No",
"pro_ass": "Y",
"itms": [
{
"num": 1,
"itm_det": {
"ty": "S",
"hsn_sc": "H724",
"txval": 5589.87,
"irt": 0.0,
"iamt": 0.0,
"crt": 87.92,
"camt": 5.7947562568E8,
"srt": 86.56,
"samt": 50.74
}
},
{
"num": 2,
"itm_det": {
"ty": "S",
"hsn_sc": "H863",
"txval": 2509.27,
"irt": 0.0,
"iamt": 0.0,
"crt": 12.99,
"camt": 26144.48,
"srt": 31.81,
"samt": 276654.5
}
}
]
}
]
},
{
"ctin": "76ABCDE2148F9Z9",
"inv": [
{
"chksum": "AflJufPlFStqKBZ",
"inum": "S008400",
"idt": "24-11-2016",
"val": 729248.16,
"pos": "6",
"rchrg": "No",
"pro_ass": "Y",
"itms": [
{
"num": 1,
"itm_det": {
"ty": "S",
"hsn_sc": "S8590",
"txval": 8196.88,
"irt": 0.0,
"iamt": 0.0,
"crt": 42.44,
"camt": 202.86,
"srt": 40.99,
"samt": 0.02
}
},
{
"num": 2,
"itm_det": {
"ty": "S",
"hsn_sc": "H357",
"txval": 6760.14,
"irt": 0.0,
"iamt": 0.0,
"crt": 23.89,
"camt": 6.8214986738E8,
"srt": 60.95,
"samt": 0.03
}
}
]
}
]
}
]
}
I need the inner loop data as new table. ie the first table should contain 2 rows which should have "ctin" and "inv" in it. Similarly the second table should have the inum details and the third table should have the item details.
With json2charp you can easily generate C# classes for parsing from the JSON. For your data you can use:
public class ItmDet
{
public string ty { get; set; }
public string hsn_sc { get; set; }
public double txval { get; set; }
public double irt { get; set; }
public double iamt { get; set; }
public double crt { get; set; }
public double camt { get; set; }
public double srt { get; set; }
public double samt { get; set; }
}
public class Itm
{
public int num { get; set; }
public ItmDet itm_det { get; set; }
}
public class Inv
{
public string inum { get; set; }
public string idt { get; set; }
public double val { get; set; }
public string pos { get; set; }
public string rchrg { get; set; }
public string pro_ass { get; set; }
public List<Itm> itms { get; set; }
public string chksum { get; set; }
}
public class B2b
{
public string ctin { get; set; }
public List<Inv> inv { get; set; }
}
public class RootObject
{
public List<B2b> b2b { get; set; }
}
You can remove the properties you do not need. Parsing with JSON.NET is like
var data = JsonConvert.DeserializeObject<RootObject>(jsonString);
I suggest you follow the bellow steps:
Copy your JSON string
Go to Visual Studio and press this menu item: Edit > Paste Special > Paste JSON as classes
Enjoy the result!
Appropriate classes:
public class Rootobject
{
public B2b[] b2b { get; set; }
}
public class B2b
{
public string ctin { get; set; }
public Inv[] inv { get; set; }
}
public class Inv
{
public string inum { get; set; }
public string idt { get; set; }
public float val { get; set; }
public string pos { get; set; }
public string rchrg { get; set; }
public string pro_ass { get; set; }
public Itm[] itms { get; set; }
public string chksum { get; set; }
}
public class Itm
{
public int num { get; set; }
public Itm_Det itm_det { get; set; }
}
public class Itm_Det
{
public string ty { get; set; }
public string hsn_sc { get; set; }
public float txval { get; set; }
public float irt { get; set; }
public float iamt { get; set; }
public float crt { get; set; }
public float camt { get; set; }
public float srt { get; set; }
public float samt { get; set; }
}
Note: Also you can use some attributes like [DataContract(Name ="...")] over the classes and [DataMember(Name = "...")] over the properties to make some differents between C# and JSON property names.
You should replace ... with a name
Reference
I'm using JavaScriptSerializer and when I try to deserialize a JSON object as
Json.Deserialize<List<HeyWatchVideo>>(Request("video"));
I get following Exception:
MissingMethodException: No parameterless constructor defined for type of 'System.String'.
I tried matching my .NET classes, and the types of their properties just as close to my json object, but still getting above exception
My JSON object:
[{
"url": "http://media.heywatch.com.s3.amazonaws.com/14/14/a0f356a48381092e6e2a34021ce86b19/11002247",
"specs": {
"size": 3808,
"video": {
"fps": 11.63,
"height": 360,
"length": 61,
"width": 640,
"aspect": 1.78,
"codec": "mpeg4",
"container": "mov",
"rotation": 0,
"bitrate": 507,
"pix_format": "yuv420p",
"stream": 0.1
},
"thumb": "http://media.heywatch.com.s3.amazonaws.com/14/14/ad59fa501d1b9e826552dfc010cf1c98/11002247.jpg",
"audio": {
"sample_rate": 11025,
"channels": 1,
"codec": "aac",
"bitrate": 38,
"synched": true,
"stream": 0
},
"mime_type": "video/mp4"
},
"title": "elves.mp4",
"filename": "11002247",
"link": "http://heywatch.com/video/21091957.bin",
"updated_at": "2013-01-14T15:44:53+01:00",
"created_at": "2013-01-14T15:44:53+01:00",
"id": 21091957
}]
My Classes
public class HeyWatchVideo
{
public DateTime Created_At { get; set; }
public string Title { get; set; }
public Dictionary<string, string> Specs { get; set; }
public DateTime Updated_At { get; set; }
public int Id { get; set; }
public string Filename { get; set; }
public string Link { get; set; }
public string Url { get; set; }
}
public class HeyWatchVideoSpecs
{
public HeyWatchVideoSpecsAudio Audio { get; set; }
public HeyWatchVideoSpecsVideo Video { get; set; }
public string Thumb { get; set; }
public string Mime_type { get; set; }
public int Size { get; set; }
}
public class HeyWatchVideoSpecsVideo
{
public int Rotation { get; set; }
public double Aspect { get; set; }
public string Container { get; set; }
public string Codec { get; set; }
public int Length { get; set; }
public int Width { get; set; }
public int Bitrate { get; set; }
public string Pix_format { get; set; }
public double Fps { get; set; }
public double Stream { get; set; }
public int Height { get; set; }
}
public class HeyWatchVideoSpecsAudio
{
public int Channels { get; set; }
public int Sample_rate { get; set; }
public string Codec { get; set; }
public bool Synched { get; set; }
public int Bitrate { get; set; }
public int Stream { get; set; }
}
What am I doing wrong here?
My problem was solved. My .Net Classes didn't match exactly as Json object. (Both are in the question)
When they were matched, this error went away