Deserialize child properties from Json - c#

I am getting the following Json from an API:
{
"name":"xyz",
"interval": "H1",
"points": [
{
"speed": 1431,
"mid": { "h": "1.07904", "l": "1.07872" }
}
]
}
And I created the models:
public class Response {
public String? Name { get; set; }
public IntervalEnum? Interval { get; set; }
public List<Point> Points { get; set; } = new List<Point>();
}
public class Point {
public Double Speed { get; set; }
[JsonPropertyName("mid.h")]
public Double High { get; set; }
[JsonPropertyName("mid.l")]
public Double Low { get; set; }
}
I am getting all values but not High and Low.
How can I parse those values?

JsonPropertyName is for the name of a property and has no relation to the path of the property. In other words, writing [JsonPropertyName("mid.h")] you are targeting a property in an object like this:
{
"mid.h": 5
}
The proper way to resolve this is to write your classes in a way that matches with the actual structure of the data:
public class Response {
public String? Name { get; set; }
public IntervalEnum? Interval { get; set; }
public List<Point> Points { get; set; } = new List<Point>();
}
public class Point {
public Double Speed { get; set; }
[JsonPropertyName("mid")]
public MidPoint MidPoint { get; set; }
}
public class MidPoint {
[JsonPropertyName("h")]
public Double High { get; set; }
[JsonPropertyName("l")]
public Double Low { get; set; }
}
If you want to make Low and High accessible in the way you wrote it, you can write a getter for it:
public class Point {
public Double Speed { get; set; }
[JsonPropertyName("mid")]
public MidPoint MidPoint { get; set; }
public Double High => MidPoint.High;
public Double Low => MidPoint.Low;
}
If this really doesn't suit you, you can implement your own JSON resolver: Can I specify a path in an attribute to map a property in my class to a child property in my JSON?

Related

Dealing with VarVector<Single> Feature column in ML.NET

I have a tricky model I'm trying to normalize for binary classification. Here's an example of the model structure. I renamed a few things just to simplify.
public class Review
{
public bool Label { get; set; }
public ReviewItem ReviewItem { get; set; }
public List<OtherItem> OtherItems { get; set; }
}
public class ReviewItem
{
public string SomeText { get; set; }
public float SomeNumber { get; set; }
public bool SomeBool { get; set; }
}
public class OtherItem
{
public string SomeDifferentText { get; set; }
public float SomeDifferentNumber { get; set; }
public bool SomeDifferentBool { get; set; }
}
There can be any number of OtherItem in the List. This is what I tried to flatten the model a bit.
public class ReviewMlModel
{
public bool Label { get; set; }
public string ReviewItem_SomeText { get; set; }
public float ReviewItem_SomeNumber { get; set; }
public bool ReviewItem_SomeBool { get; set; }
public string[] OtherItem_SomeDifferentText { get; set; }
public float[] OtherItem_SomeDifferentNumber { get; set; }
public bool[] OtherItem_SomeDifferentBool { get; set; }
}
From there I tried this to normalize it:
var data = mlContext.Data.LoadFromEnumerable(allReviews);
var dataPrepEstimator = mlContext.Transforms.Text.FeaturizeText("ReviewItem_SomeText")
.Append(mlContext.Transforms.Text.FeaturizeText("OtherItem_SomeDifferentText"))
.Append(mlContext.Transforms.Conversion.ConvertType("ReviewItem_SomeBool"))
.Append(mlContext.Transforms.Conversion.ConvertType("OtherItem_SomeDifferentBool"))
.Append(mlContext.Transforms.Concatenate("Features",
"ReviewItem_SomeText", "OtherItem_SomeDifferentText", "ReviewItem_SomeBool",
"OtherItem_SomeDifferentBool", "ReviewItem_SomeNumber", "OtherItem_SomeDifferentNumber"));
var transformedData = dataPrepEstimator.Fit(data).Transform(data);
var model = mlContext.BinaryClassification.Trainers.AveragedPerceptron()
.Fit(transformedData);
This gives me the exception on the line where I try to create the model:
Schema mismatch for feature column 'Features': expected Vector<Single>, got VarVector<Single> (Parameter 'inputSchema')
I'm guessing this is due to the fact these arrays all have variable lengths, but I don't see a way to transform the VarVector. Do I need to go make the original allReviews enumerable have the same length array for every array? Or am I way off track with how I flattened the original model?
Looks like it was a combination of changing the arrays to all have the same size, as well as adding [VectorType(size)] attribute to all the array properties.

JSON invalid format

I'm trying to deserialize the JSON from the weather API, I have created C# classes but I can't seem to get it working. Most sites display this JSON as invalid format, so I'm not really sure whats wrong with it. Here is the JSON string and my class for Deserializing.Visual studio displays it like any other regular JSON.
"[\"cod\":\"200\",\"message\":0,\"cnt\":40,\"list\":[[\"dt\":1574175600,\"main\":[\"temp\":284.79,\"temp_min\":282.63,\"temp_max\":284.79,\"pressure\":1021,\"sea_level\":1021,\"grnd_level\":958,\"humidity\":88,\"temp_kf\":2.16],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3,\"deg\":51],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-19 15:00:00\"],[\"dt\":1574186400,\"main\":[\"temp\":282.92,\"temp_min\":281.3,\"temp_max\":282.92,\"pressure\":1021,\"sea_level\":1021,\"grnd_level\":958,\"humidity\":94,\"temp_kf\":1.62],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.93,\"deg\":53],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-19 18:00:00\"],[\"dt\":1574197200,\"main\":[\"temp\":282.6,\"temp_min\":281.52,\"temp_max\":282.6,\"pressure\":1021,\"sea_level\":1021,\"grnd_level\":957,\"humidity\":93,\"temp_kf\":1.08],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.84,\"deg\":64],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-19 21:00:00\"],[\"dt\":1574208000,\"main\":[\"temp\":281.67,\"temp_min\":281.13,\"temp_max\":281.67,\"pressure\":1020,\"sea_level\":1020,\"grnd_level\":956,\"humidity\":94,\"temp_kf\":0.54],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.83,\"deg\":65],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-20 00:00:00\"],[\"dt\":1574218800,\"main\":[\"temp\":280.97,\"temp_min\":280.97,\"temp_max\":280.97,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":96,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.9,\"deg\":57],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-20 03:00:00\"],[\"dt\":1574229600,\"main\":[\"temp\":280.72,\"temp_min\":280.72,\"temp_max\":280.72,\"pressure\":1016,\"sea_level\":1016,\"grnd_level\":953,\"humidity\":96,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":90],\"wind\":[\"speed\":3.65,\"deg\":65],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-20 06:00:00\"],[\"dt\":1574240400,\"main\":[\"temp\":282.34,\"temp_min\":282.34,\"temp_max\":282.34,\"pressure\":1015,\"sea_level\":1015,\"grnd_level\":953,\"humidity\":91,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":1.32,\"deg\":78],\"rain\":[\"3h\":1.19],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-20 09:00:00\"],[\"dt\":1574251200,\"main\":[\"temp\":283.44,\"temp_min\":283.44,\"temp_max\":283.44,\"pressure\":1015,\"sea_level\":1015,\"grnd_level\":952,\"humidity\":87,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.16,\"deg\":79],\"rain\":[\"3h\":1.94],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-20 12:00:00\"],[\"dt\":1574262000,\"main\":[\"temp\":282.5,\"temp_min\":282.5,\"temp_max\":282.5,\"pressure\":1015,\"sea_level\":1015,\"grnd_level\":952,\"humidity\":90,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.21,\"deg\":75],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-20 15:00:00\"],[\"dt\":1574272800,\"main\":[\"temp\":281.27,\"temp_min\":281.27,\"temp_max\":281.27,\"pressure\":1016,\"sea_level\":1016,\"grnd_level\":953,\"humidity\":88,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"]],\"clouds\":[\"all\":80],\"wind\":[\"speed\":1.94,\"deg\":105],\"rain\":[\"3h\":0.38],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-20 18:00:00\"],[\"dt\":1574283600,\"main\":[\"temp\":280.14,\"temp_min\":280.14,\"temp_max\":280.14,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":90,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"]],\"clouds\":[\"all\":0],\"wind\":[\"speed\":1.72,\"deg\":131],\"rain\":[\"3h\":0.19],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-20 21:00:00\"],[\"dt\":1574294400,\"main\":[\"temp\":279.7,\"temp_min\":279.7,\"temp_max\":279.7,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":89,\"temp_kf\":0],\"weather\":[[\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01n\"]],\"clouds\":[\"all\":0],\"wind\":[\"speed\":1.53,\"deg\":122],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-21 00:00:00\"],[\"dt\":1574305200,\"main\":[\"temp\":279.29,\"temp_min\":279.29,\"temp_max\":279.29,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":89,\"temp_kf\":0],\"weather\":[[\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01n\"]],\"clouds\":[\"all\":0],\"wind\":[\"speed\":1.71,\"deg\":110],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-21 03:00:00\"],[\"dt\":1574316000,\"main\":[\"temp\":278.99,\"temp_min\":278.99,\"temp_max\":278.99,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":90,\"temp_kf\":0],\"weather\":[[\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"]],\"clouds\":[\"all\":0],\"wind\":[\"speed\":1.76,\"deg\":106],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-21 06:00:00\"],[\"dt\":1574326800,\"main\":[\"temp\":284.12,\"temp_min\":284.12,\"temp_max\":284.12,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":71,\"temp_kf\":0],\"weather\":[[\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"]],\"clouds\":[\"all\":0],\"wind\":[\"speed\":1.91,\"deg\":94],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-21 09:00:00\"],[\"dt\":1574337600,\"main\":[\"temp\":286.38,\"temp_min\":286.38,\"temp_max\":286.38,\"pressure\":1016,\"sea_level\":1016,\"grnd_level\":953,\"humidity\":62,\"temp_kf\":0],\"weather\":[[\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"]],\"clouds\":[\"all\":21],\"wind\":[\"speed\":2.19,\"deg\":66],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-21 12:00:00\"],[\"dt\":1574348400,\"main\":[\"temp\":282.01,\"temp_min\":282.01,\"temp_max\":282.01,\"pressure\":1016,\"sea_level\":1016,\"grnd_level\":953,\"humidity\":91,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.67,\"deg\":46],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-21 15:00:00\"],[\"dt\":1574359200,\"main\":[\"temp\":280.86,\"temp_min\":280.86,\"temp_max\":280.86,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":94,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":86],\"wind\":[\"speed\":2.78,\"deg\":49],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-21 18:00:00\"],[\"dt\":1574370000,\"main\":[\"temp\":280.55,\"temp_min\":280.55,\"temp_max\":280.55,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":92,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.52,\"deg\":56],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-21 21:00:00\"],[\"dt\":1574380800,\"main\":[\"temp\":280.04,\"temp_min\":280.04,\"temp_max\":280.04,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":953,\"humidity\":95,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.6,\"deg\":53],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-22 00:00:00\"],[\"dt\":1574391600,\"main\":[\"temp\":280.1,\"temp_min\":280.1,\"temp_max\":280.1,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":953,\"humidity\":96,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.54,\"deg\":49],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-22 03:00:00\"],[\"dt\":1574402400,\"main\":[\"temp\":281.29,\"temp_min\":281.29,\"temp_max\":281.29,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":953,\"humidity\":97,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":2.24,\"deg\":38],\"rain\":[\"3h\":0.44],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-22 06:00:00\"],[\"dt\":1574413200,\"main\":[\"temp\":280.84,\"temp_min\":280.84,\"temp_max\":280.84,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":93,\"temp_kf\":0],\"weather\":[[\"id\":501,\"main\":\"Rain\",\"description\":\"moderate rain\",\"icon\":\"10d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.21,\"deg\":33],\"rain\":[\"3h\":3.88],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-22 09:00:00\"],[\"dt\":1574424000,\"main\":[\"temp\":279.71,\"temp_min\":279.71,\"temp_max\":279.71,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":93,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.37,\"deg\":41],\"rain\":[\"3h\":2.81],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-22 12:00:00\"],[\"dt\":1574434800,\"main\":[\"temp\":278.48,\"temp_min\":278.48,\"temp_max\":278.48,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":93,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.59,\"deg\":49],\"rain\":[\"3h\":1.88],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-22 15:00:00\"],[\"dt\":1574445600,\"main\":[\"temp\":278.11,\"temp_min\":278.11,\"temp_max\":278.11,\"pressure\":1019,\"sea_level\":1019,\"grnd_level\":955,\"humidity\":93,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.55,\"deg\":49],\"rain\":[\"3h\":0.81],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-22 18:00:00\"],[\"dt\":1574456400,\"main\":[\"temp\":278.1,\"temp_min\":278.1,\"temp_max\":278.1,\"pressure\":1019,\"sea_level\":1019,\"grnd_level\":955,\"humidity\":93,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.67,\"deg\":49],\"rain\":[\"3h\":1.38],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-22 21:00:00\"],[\"dt\":1574467200,\"main\":[\"temp\":277.43,\"temp_min\":277.43,\"temp_max\":277.43,\"pressure\":1020,\"sea_level\":1020,\"grnd_level\":955,\"humidity\":93,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.15,\"deg\":47],\"rain\":[\"3h\":0.75],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-23 00:00:00\"],[\"dt\":1574478000,\"main\":[\"temp\":277.23,\"temp_min\":277.23,\"temp_max\":277.23,\"pressure\":1019,\"sea_level\":1019,\"grnd_level\":955,\"humidity\":92,\"temp_kf\":0],\"weather\":[[\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"]],\"clouds\":[\"all\":98],\"wind\":[\"speed\":3.5,\"deg\":59],\"rain\":[\"3h\":0.56],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-23 03:00:00\"],[\"dt\":1574488800,\"main\":[\"temp\":276.54,\"temp_min\":276.54,\"temp_max\":276.54,\"pressure\":1020,\"sea_level\":1020,\"grnd_level\":955,\"humidity\":95,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":98],\"wind\":[\"speed\":3.32,\"deg\":59],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-23 06:00:00\"],[\"dt\":1574499600,\"main\":[\"temp\":278.93,\"temp_min\":278.93,\"temp_max\":278.93,\"pressure\":1019,\"sea_level\":1019,\"grnd_level\":955,\"humidity\":86,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.36,\"deg\":56],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-23 09:00:00\"],[\"dt\":1574510400,\"main\":[\"temp\":280.06,\"temp_min\":280.06,\"temp_max\":280.06,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":80,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.74,\"deg\":53],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-23 12:00:00\"],[\"dt\":1574521200,\"main\":[\"temp\":277.41,\"temp_min\":277.41,\"temp_max\":277.41,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":954,\"humidity\":89,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.84,\"deg\":55],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-23 15:00:00\"],[\"dt\":1574532000,\"main\":[\"temp\":276.83,\"temp_min\":276.83,\"temp_max\":276.83,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":92,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":96],\"wind\":[\"speed\":3.75,\"deg\":56],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-23 18:00:00\"],[\"dt\":1574542800,\"main\":[\"temp\":277.04,\"temp_min\":277.04,\"temp_max\":277.04,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":90,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":100],\"wind\":[\"speed\":3.55,\"deg\":61],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-23 21:00:00\"],[\"dt\":1574553600,\"main\":[\"temp\":275.99,\"temp_min\":275.99,\"temp_max\":275.99,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":96,\"temp_kf\":0],\"weather\":[[\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":81],\"wind\":[\"speed\":3.19,\"deg\":55],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-24 00:00:00\"],[\"dt\":1574564400,\"main\":[\"temp\":276.56,\"temp_min\":276.56,\"temp_max\":276.56,\"pressure\":1017,\"sea_level\":1017,\"grnd_level\":953,\"humidity\":96,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"]],\"clouds\":[\"all\":91],\"wind\":[\"speed\":3.02,\"deg\":62],\"sys\":[\"pod\":\"n\"],\"dt_txt\":\"2019-11-24 03:00:00\"],[\"dt\":1574575200,\"main\":[\"temp\":277.36,\"temp_min\":277.36,\"temp_max\":277.36,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":95,\"temp_kf\":0],\"weather\":[[\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":86],\"wind\":[\"speed\":2.99,\"deg\":62],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-24 06:00:00\"],[\"dt\":1574586000,\"main\":[\"temp\":280.51,\"temp_min\":280.51,\"temp_max\":280.51,\"pressure\":1018,\"sea_level\":1018,\"grnd_level\":954,\"humidity\":82,\"temp_kf\":0],\"weather\":[[\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"]],\"clouds\":[\"all\":74],\"wind\":[\"speed\":3.05,\"deg\":64],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-24 09:00:00\"],[\"dt\":1574596800,\"main\":[\"temp\":282.87,\"temp_min\":282.87,\"temp_max\":282.87,\"pressure\":1016,\"sea_level\":1016,\"grnd_level\":953,\"humidity\":74,\"temp_kf\":0],\"weather\":[[\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03d\"]],\"clouds\":[\"all\":42],\"wind\":[\"speed\":3.21,\"deg\":50],\"sys\":[\"pod\":\"d\"],\"dt_txt\":\"2019-11-24 12:00:00\"]],\"city\":[\"id\":787657,\"name\":\"Nis\",\"coord\":[\"lat\":43.3247,\"lon\":21.9033],\"country\":\"RS\",\"timezone\":3600,\"sunrise\":1574141412,\"sunset\":1574175927]]"
And my class for deserializing:
public class WeatherInfo
{
public class root
{
public string cod { get; set; }
public string message { get; set; }
public string cnt { get; set; }
public allLists list { get; set; }
public city city { get; set; }
}
public class allLists
{
allinfo[] allinfos { get; set; }
}
public class allinfo
{
public string dt { get; set; }
public main main { get; set; }
public allWeathers weather { get; set; }
public string dt_txt { get; set; }
}
public class main
{
public double temp { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public double pressure { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public double humidity { get; set; }
public double temp_kf { get; set; }
}
public class allWeathers
{
public weather[] weathers { 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 clouds
{
public double all { get; set; }
}
public class wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class sys
{
public string pod { get; set; }
}
public class city
{
public int id { get; set; }
public string name { get; set; }
public coord coord { get; set; }
public string country { get; set; }
public string timezone { get; set; }
public string sunrise { get; set; }
public string sunset { get; set; }
}
public class coord
{
public double lat { get; set; }
public double lon { get; set; }
}
}
Your JSON is not valid.
The main thing is that you are using brackets [] where you should be using braces {}.
In JSON [] is an array, while {} is an object. There are a couple of places where you need to have an array (e.g. list) so you can't simply do a find-replace to fix this.
An easy way to resolve this in the future is to create an instance of your WeatherInfo class, populate it with data, and then serialize it to JSON. You can then compare that with the JSON you have (using a diff tool like KDiff or WinMerge) to identify where your source JSON is different from your generated JSON.
A valid JSON always starts with a [ or a { and ends with the same, where square bracket [ represents an array and curly bracket { represents an object.
Looking at your C# class, your JSON lacks the curly brace to start with for objects and is also missing names for the arrays like allinfos.
You should start building your JSON like this:
var json = JToken.Parse
("{\"cod\":\"200\",\"message\":0,\"cnt\":40,\"list\":{\"allinfos\":[{\"dt\":1574175600, \"main\":{\"temp\":284.79,\"temp_min\":282.63,\"temp_max\":284.79,\"pressure\":1021,\"sea_level\":1021,\"grnd_level\":958,\"humidity\":88,\"temp_kf\":2.16}}]}}").ToString(Formatting.Indented);
This will give a formatted, easy to read output like this:
{
"cod": "200",
"message": 0,
"cnt": 40,
"list": {
"allinfos": [
{
"dt": 1574175600,
"main": {
"temp": 284.79,
"temp_min": 282.63,
"temp_max": 284.79,
"pressure": 1021,
"sea_level": 1021,
"grnd_level": 958,
"humidity": 88,
"temp_kf": 2.16
}
}
]
}
}
Update: The JSON was actually valid, the problem is that I was trying to work with debugging version of the string. The debugger automatically adds escape characters and "" quotes. Also some of the classes I had didn't follow up the JSON format correctly, so that error stopped me from seeing the real issue here.

When consuming a web service, how do I know the type of the data being returned via JSON?

I have no previous experience with JSON or using web services, however I'm trying to consume a web service that returns meteorological information.
Here's the documentation on the API that I'm trying to use.
This API gives me data serialized with JSON. I did some reading into JSON, and from what I understand, the best way to access this serialized data after I download it would be to de-serialize it into an object with matching properties and types. Did I get this part right? I don't understand however in this case how am I supposed to accurately know the types of the data returned via JSON.
In the API that I mentioned before I got this example of a response from the API in JSON:
{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}
What I did was, on Visual Studio I used the "Paste Special" > "Paste as JSON classes" option, which created these classes for me:
public class Rootobject
{
public Coord coord { get; set; }
public Weather[] weather { get; set; }
public string _base { get; set; }
public Main main { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public Rain rain { 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 float lon { get; set; }
public float lat { get; set; }
}
public class Main
{
public float temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public float temp_min { get; set; }
public float temp_max { get; set; }
}
public class Wind
{
public float speed { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Rain
{
public int _3h { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public float message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { 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; }
}
The problem is that when I request data using the HttpClient, when I try to de-serialize the response I get I few errors regarding mismatching data types, like for example, float data being assign to properties of the type int.
Here's a snippet of my code:
string json = await client.GetStringAsync("weather?q=London,uk&appid=010101010101010101101");
Rootobject currentWeather = new Rootobject();
currentWeather = JsonConvert.DeserializeObject<Rootobject>(json);
MessageBox.Show(currentWeather.name);
I understand that in this case I could change the types of the properties in my classes to match what is being returned by the API, however that doesn't feel right to me, mainly because seems like it could be a source of troubles and unpredictable behavior. Am I doing this right? Maybe I'm missing something in the API documentation, shouldn't they provide the type of the data being returned?
Correct : De-serialize it into an object with matching properties and types.
First check the API documentation for the types, and if that is not comprehensiive enough I would consider changing your types to match what you infer from the JSON.
A value of 289.9 is a float.
A value of 1435650870 can be stored an int.
A value of AU can be a string/enum.
Edit:
I checked the API documentation you linked to and don't see anywhere were it explicitly states the types of data returned.
Edit 2:
Answering your question more directly, "how am I supposed to accurately know the types of the data returned via JSON?" (thanks #CodeCaster), without finding that information in documentation I don't think you can.
But I feel you can get 99.999% close by just looking at historical data returned.
If it is satisfied for you to use dynamic you can try this snippet
string json = await client.GetStringAsync("weather?q=London,uk&appid=010101010101010101101");
dynamic currentWeather= JObject.Parse(json);
MessageBox.Show(currentWeather.name);
You will find more details in documentation

Create "list" of related properties and pass to method

I have a class with many related properties that need I need to run some simple calculations on. The general structure is:
public class SpecialItem
{
public decimal Value { get; set; }
}
public class Processor
{
public decimal CalcA { get; set; }
public decimal CalcB { get; set; }
public decimal CalcC { get; set; }
public SpecialItem ItemA { get; set; }
public SpecialItem ItemB { get; set; }
public SpecialItem ItemC { get; set; }
public decimal PrevCalcA { get; set; }
public decimal PrevCalcB { get; set; }
public decimal PrevCalcC { get; set; }
public decimal DifferenceA { get; set; }
public decimal DifferenceB { get; set; }
public decimal DifferenceC { get; set; }
private void Subtract()
{//TODO}
}
I need to calculate DifferenceA = PrevCalcA - ItemA.Value - CalcA, then DifferenceB = PrevCalcB - ItemB.Value - CalcB , and so on. What is the best way to do this, ideally having a simple subtractor method (ex: private void Subtract(decimal a, decimal b, decimal c) that can sequentially calculate the Difference properties as you feed it a "list" containing groups of 3 other properties?
The reason I have all these properties is that they each indvidually bind to a ViewModel.
Thanks!
It seems to me that the simplest thing to do is redefine the DifferenceX properties like this:
public decimal DifferenceA
{
get
{
return this.PrevCalcA - this.ItemA.Value - this.CalcA;
}
}
public decimal DifferenceB
{
get
{
return this.PrevCalcB - this.ItemB.Value - this.CalcB;
}
}
public decimal DifferenceC
{
get
{
return this.PrevCalcC - this.ItemC.Value - this.CalcC;
}
}
Or am I missing something in your question?

Json String doesn't deserialize

I have the following JSON:
{"success":1,
"return":{
"funds": {
"usd":0,
"btc":0,
"ltc":0
},
"rights": {
"info":1,
"trade":0,
"withdraw":0
},
"transaction_count":1,
"open_orders":0,
"server_time":1406470221
}
}
I'm trying to deserialize it with:
JsonConvert.DeserializeObject<UserInfo>(jsonString);
Classes are as follows:
public class UserInfo
{
[JsonProperty("transaction_count")]
public int TransactionCount { get; set; }
[JsonProperty("open_orders")]
public int OpenOrders { get; set; }
[JsonProperty("server_time")]
public int ServerTime { get; set; }
[JsonProperty("funds")]
public Funds Funds { get; set; }
[JsonProperty("rights")]
public Rights Rights { get; set; }
}
public class Funds
{
[JsonProperty("btc")]
public decimal Btc { get; set; }
[JsonProperty("ltc")]
public decimal Ltc { get; set; }
[JsonProperty("usd")]
public decimal Usd { get; set; }
};
public class Rights
{
[JsonProperty("info")]
public bool Info { get; set; }
[JsonProperty("trade")]
public bool Trade { get; set; }
[JsonProperty("withdraw")]
public bool Withdraw { get; set; }
}
I tried not using the attributes and other tutorials, but nothing seems to work.. =(
Why doesn't it work? I don't know how to set return as a root, for example. Is it possible?
Thank you.
Try this:
public class Funds
{
public int usd { get; set; }
public int btc { get; set; }
public int ltc { get; set; }
}
public class Rights
{
public int info { get; set; }
public int trade { get; set; }
public int withdraw { get; set; }
}
public class Return
{
public Funds funds { get; set; }
public Rights rights { get; set; }
public int transaction_count { get; set; }
public int open_orders { get; set; }
public int server_time { get; set; }
}
public class RootObject
{
public int success { get; set; }
public Return #return { get; set; }
}
And to deserialize:
JsonConvert.DeserializeObject<RootObject>(jsonString);
Reference: http://json2csharp.com/
Your JSON isn't well-formed.
{"success":1,
"return":{
"funds": <-- missing {
"usd":0,
"btc":0,
"ltc":0
},
"rights": <-- missing {
"info":1,
"trade":0,
"withdraw":0
},
"transaction_count":1,
"open_orders":0,
"server_time":1406470221
}
}
Regarding your question about setting root, try writing a simple wrapper class to represent your actual JSON hierarchy, then deserialize the whole string as is, it will work if your object graph maps to the JSON.
EDIT:See #Crasher's answer, it is exactly what I mean, with an actual sample (though I didnt verify, I will upvote it), though #dcastro's answer is the cleanest.
An alternative to dcastro's exact example, if you use JObject to handle the dynamic root, you can also use JObject.First to either avoid naming the root, or allow root to change. Please upvote his answer, I'm just providing a dynamic alternative.
var root = JObject.Parse(jsonStr).First;
You don't need to create a wrapper/root class just for this, as others answers suggest.
You can parse the whole thing into a JObject, and then convert just the return node into a UserInfo object.
JObject obj = JObject.Parse(jsonStr);
var userInfo = obj["return"].ToObject<UserInfo>();

Categories

Resources