Azure JSON Response Deserialization C# - c#

My JSON Response is shown below,
[
{
"faceRectangle": {
"top": 214,
"left": 472,
"width": 450,
"height": 450
},
"faceAttributes": {
"age": 19.0,
"emotion": {
"anger": 0.0,
"contempt": 0.0,
"disgust": 0.0,
"fear": 0.0,
"happiness": 0.0,
"neutral": 0.996,
"sadness": 0.003,
"surprise": 0.001
}
}
}
]
My C# class is as follows:
public class Output
{
public List<FaceRectangle> FaceRectangles { get; set; }
public List<FaceAttribute> faceAttributes { get; set; }
}
public class FaceAttribute
{
public List<Emotion> Emotions { get; set; }
public int age { get; set; }
}
public class Emotion {
public float anger { get; set; }
public float contempt { get; set; }
public float disgust { get; set; }
public float fear { get; set; }
public float happiness { get; set; }
public float neutral { get; set; }
public float sadness { get; set; }
public float surprise { get; set; }
}
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
However, when I am trying to deserialize the above response, I am getting the following error.
Error code is shown as "Not supported for deserialization of an array"
enter image description here
Can anyone help me with this?
Thank you!

Try using these classes created from json2csharp:
public class FaceRectangle
{
public int Top { get; set; }
public int Left { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public class Emotion
{
public double Anger { get; set; }
public double Contempt { get; set; }
public double Disgust { get; set; }
public double Fear { get; set; }
public double Happiness { get; set; }
public double Neutral { get; set; }
public double Sadness { get; set; }
public double Surprise { get; set; }
}
public class FaceAttributes
{
public double Age { get; set; }
public Emotion Emotion { get; set; }
}
public class RootObject
{
public FaceRectangle FaceRectangle { get; set; }
public FaceAttributes FaceAttributes { get; set; }
}
Then you can deserialize your JSON Array response into List<RootObject using Newtonsoft.Json NuGet Package:
using Newtonsoft.Json;
...
var json = #"[{""faceRectangle"": {""top"": 214,""left"": 472,""width"": 450,""height"": 450},""faceAttributes"": {""age"": 19.0,""emotion"": {""anger"": 0.0,""contempt"": 0.0,""disgust"": 0.0,""fear"": 0.0,""happiness"": 0.0,""neutral"": 0.996,""sadness"": 0.003,""surprise"": 0.001}}}]";
var deserializedJson = JsonConvert.DeserializeObject<List<RootObject>>(json);

Related

Not sure why Open Weather API C# Call is not working

I am having difficulty replacing the Dark Sky API with Open weather. GPS Location works and is received through another azure function.The Dark Sky Api call still works too. It might be something little but I'm not sure what's wrong. The only significant difference from the Dark Sky Api is the data model. Yet, Postman keeps responding with null values.
Postman response:
{
"id": 0,
"main": null,
"description": null,
"icon": null
}
public interface IWeatherService
{
Task<Weather> GetWeatherAsync(double latitude, double longitude);
}
public class WeatherService : IWeatherService
{
private readonly HttpClient _client;
private readonly ILogger<IWeatherService> _logger;
public WeatherService(HttpClient client, ILogger<IWeatherService> logger)
{
_client = client;
_client.BaseAddress = new Uri("https://api.openweathermap.org/data/2.5/weather?");
_client.Timeout = TimeSpan.FromSeconds(10);
_logger = logger;
}
public async Task<Weather> GetWeatherAsync(double latitude, double longitude)
{
Weather retval = null;
try
{
if (latitude.Equals(0) || longitude.Equals(0))
return null;
var key = "HIDDEN";
// var API = Environment.GetEnvironmentVariable("WeatherKey");
var exclude = "minutely,hourly,daily,alerts";
var units = "imperial";
var requestUrl = $"&lat={latitude}&lon={longitude}&exclude={exclude}&units={units}&appid={key}";
var response = await _client.GetAsync(requestUrl);
var result = response.Content.ReadAsStringAsync().Result;
var weather = JsonConvert.DeserializeObject<Weather>(result);
retval = weather;
//}
//handler.Dispose();
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error in {nameof(GetWeatherAsync)}");
}
return retval;
}
The new model:
namespace WeatherWebAPI.Models
{
using Newtonsoft.Json;yy
public partial class Temperatures
{
[JsonProperty("coord")]
public Coord Coord { get; set; }
[JsonProperty("weather")]
public Weather[] Weather { get; set; }
[JsonProperty("base")]
public string Base { get; set; }
[JsonProperty("main")]
public Main Main { get; set; }
[JsonProperty("visibility")]
public long Visibility { get; set; }
[JsonProperty("wind")]
public Wind Wind { get; set; }
[JsonProperty("clouds")]
public Clouds Clouds { get; set; }
[JsonProperty("dt")]
public long Dt { get; set; }
[JsonProperty("sys")]
public Sys Sys { get; set; }
[JsonProperty("timezone")]
public long Timezone { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("cod")]
public long Cod { get; set; }
}
public partial class Clouds
{
[JsonProperty("all")]
public long All { get; set; }
}
public partial class Coord
{
[JsonProperty("lon")]
public double Lon { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
}
public partial class Main
{
[JsonProperty("temp")]
public double Temp { get; set; }
[JsonProperty("feels_like")]
public double FeelsLike { get; set; }
[JsonProperty("temp_min")]
public double TempMin { get; set; }
[JsonProperty("temp_max")]
public double TempMax { get; set; }
[JsonProperty("pressure")]
public long Pressure { get; set; }
[JsonProperty("humidity")]
public long Humidity { get; set; }
}
public partial class Sys
{
[JsonProperty("type")]
public long Type { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("message")]
public double Message { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("sunrise")]
public long Sunrise { get; set; }
[JsonProperty("sunset")]
public long Sunset { get; set; }
}
public partial class Weather
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("main")]
public string Main { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("icon")]
public string Icon { get; set; }
}
public partial class Wind
{
[JsonProperty("speed")]
public double Speed { get; set; }
[JsonProperty("deg")]
public long Deg { get; set; }
}
}
The Old Model:
namespace WeatherWebAPI.Models
{
#pragma warning disable IDE1006 // Naming Styles
public class Response
{
public double latitude { get; set; }
public double longitude { get; set; }
public string timezone { get; set; }
public DataPoint currently { get; set; }
}
public class DataPoint
{
public double apparentTemperatureHigh { get; set; }
public string apparentTemperatureHighTime { get; set; }
public double apparentTemperatureLow { get; set; }
public string apparentTemperatureLowTime { get; set; }
public double apparentTemperatureMax { get; set; }
public string apparentTemperatureMaxTime { get; set; }
public double apparentTemperatureMin { get; set; }
public string apparentTemperatureMinTime { get; set; }
public long time { get; set; }
public string summary { get; set; }
public string icon { get; set; }
public string sunriseTime { get; set; }
public string sunsetTime { get; set; }
public double nearestStormDistance { get; set; }
public double nearestStormBearing { get; set; }
public double precipIntensity { get; set; }
public double precipIntensityMax { get; set; }
public string precipIntensityMaxTime { get; set; }
public string precipIntensityError { get; set; }
public double preciProbability { get; set; }
public string precipType { get; set; }
public double temperature { get; set; }
public double temperatureHigh { get; set; }
public string temperatureHighTime { get; set; }
public double temperatureLow { get; set; }
public string temperatureLowTime { get; set; }
public double temperatureMax { get; set; }
public string temperatureMaxTime { get; set; }
public double temperatureMin { get; set; }
public string temperatureMinTime { get; set; }
public double apparentTemperature { get; set; }
public double dewPoint { get; set; }
public double humidity { get; set; }
public double pressure { get; set; }
public double windSpeed { get; set; }
public double windGust { get; set; }
public string windGustTime { get; set; }
public double windBearing { get; set; }
public double cloudCover { get; set; }
public double uvIndex { get; set; }
public double uvIndexTime { get; set; }
public double visibility { get; set; }
public double ozone { get; set; }
public double moonPhase { get; set; }
}
#pragma warning restore IDE1006 // Naming Styles
}

How to get data from JSON and convert into the object? [duplicate]

This question already has answers here:
Deserializing JSON data to C# using JSON.NET
(7 answers)
Closed 5 years ago.
I'm trying to receive data from request response which is JSON like this:
{
"cnt":1,
"list":[{
"coord":{
"lon":18.55,
"lat":50.11
},
"sys":{
"type":1,
"id":5356,
"message":0.0095,
"country":"PL",
"sunrise":1496630290,
"sunset":1496688673
},
"weather":[{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"
}],
"main":{
"temp":293.71,
"pressure":1014,
"humidity":42,
"temp_min":293.15,
"temp_max":294.15
},
"visibility":10000,
"wind":{
"speed":4.1,
"deg":60
},
"clouds":{
"all":0
},
"dt":1496686603,
"id":7531758,
"name":"Rybnik"
}]
}
I'm trying to use JSON.NET to deserialize JSON and receive data. But I don't know exactly how to do it properly.
I tried to achieve this by using my class method:
public Rootobject DeserializeJSON()
{
private JObject responseObject;
private JToken responseToken;
private JArray responseArray;
responseObject = JObject.Parse(json);
Rootobject Weather = new Rootobject();
responseArray = (JArray)responseObject.SelectToken("list");
responseToken = (JToken)responseArray.SelectToken("main");
Weather = responseToken.ToObject<Rootobject>();
return Weather;
}
But It doesn't seems to work. In this case, I tried to receive data from "main" property in JSON and convert in to my class object:
class Main
{
public float temp { get; set; }
public float pressure { get; set; }
public float humidity { get; set; }
public float temp_min { get; set; }
public float temp_max { get; set; }
}
Could you explain me, how it should works and where's my fault, please?
Initially, you need to declare the following classes:
public class Coord
{
[JsonProperty("lon")]
public double Lon { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
}
public class Sys
{
[JsonProperty("type")]
public int Type { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("message")]
public double Message { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("sunrise")]
public int Sunrise { get; set; }
[JsonProperty("sunset")]
public int Sunset { get; set; }
}
public class Weather
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("main")]
public string Main { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("icon")]
public string Icon { get; set; }
}
public class Main
{
[JsonProperty("temp")]
public double Temp { get; set; }
[JsonProperty("pressure")]
public int Pressure { get; set; }
[JsonProperty("humidity")]
public int Humidity { get; set; }
[JsonProperty("temp_min")]
public double TempMin { get; set; }
[JsonProperty("temp_max")]
public double TempMax { get; set; }
}
public class Wind
{
[JsonProperty("speed")]
public double Speed { get; set; }
[JsonProperty("deg")]
public int Deg { get; set; }
}
public class Clouds
{
[JsonProperty("all")]
public int All { get; set; }
}
public class List
{
[JsonProperty("coord")]
public Coord Coord { get; set; }
[JsonProperty("sys")]
public Sys Sys { get; set; }
[JsonProperty("weather")]
public IList<Weather> Weather { get; set; }
[JsonProperty("main")]
public Main Main { get; set; }
[JsonProperty("visibility")]
public int Visibility { get; set; }
[JsonProperty("wind")]
public Wind Wind { get; set; }
[JsonProperty("clouds")]
public Clouds Clouds { get; set; }
[JsonProperty("dt")]
public int Dt { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class Example
{
[JsonProperty("cnt")]
public int Cnt { get; set; }
[JsonProperty("list")]
public IList<List> List { get; set; }
}
Then you can deserialize your json as below:
var example = JsonConvert.DeserializeObject<Example>(jsonString)
where jsonString is your JSON.
PS. I derived the above classes using jsonutils and your json string.

Deserializing JSON data to C#

Could U help me Deserializing JSON to C#? I just eat my teeth on it.. =.=.
I find many methods how to do not solve this xD,
I dont want share that with U.
I wish to wait for your suggestions.
Usefull links:
http://jsonviewer.stack.hu/
http://json2csharp.com/
JSON looks..
[
{
"faceId":"626f5974-1d63-40d4-98f1-7e6a7df13dba",
"faceRectangle":{
"top":108,
"left":699,
"width":208,
"height":208
},
"faceAttributes":{
"smile":0.973,
"gender":"male",
"age":25.7,
"emotion":{
"anger":0.0,
"contempt":0.026,
"disgust":0.0,
"fear":0.0,
"happiness":0.973,
"neutral":0.001,
"sadness":0.0,
"surprise":0.0
}
}
},
{
"faceId":"bc051f1d-9a64-4e86-bf95-2af1de21d316",
"faceRectangle":{
"top":104,
"left":634,
"width":114,
"height":114
},
"faceAttributes":{
"smile":0.074,
"gender":"male",
"age":17.4,
"emotion":{
"anger":0.003,
"contempt":0.003,
"disgust":0.001,
"fear":0.002,
"happiness":0.074,
"neutral":0.828,
"sadness":0.079,
"surprise":0.01
}
}
}
]
First of all you need classes to which you want to deserialize, so you can create manually or the fastest and easiest way - you can copy json string that you have, then in visual studio
Edit -> Paste Special -> Paste JSON as Classes
Now you have structure for deserializing.Then you can for example use Newtonsoft.Json (which you can download from NuGet). There is JsonConvert.DeserializeObject<T>() generic method, that will do all deserialization work for you.
Also, if you want use your own property names in the class structure, you can use [JsonProperty("Name")] attribute, which will change the names of properties when they are serialized to JSON and vice versa.
Your JSON document represents an array so you must deserialize it to a collection
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string jsonString = "[ {\"faceId\": \"626f5974-1d63-40d4-98f1-7e6a7df13dba\",\"faceRectangle\": { \"top\": 108, \"left\": 699, \"width\": 208, \"height\": 208 },\"faceAttributes\": { \"smile\": 0.973, \"gender\": \"male\", \"age\": 25.7, \"emotion\": { \"anger\": 0.0, \"contempt\": 0.026, \"disgust\": 0.0, \"fear\": 0.0, \"happiness\": 0.973, \"neutral\": 0.001, \"sadness\": 0.0, \"surprise\": 0.0 } }},{\"faceId\": \"bc051f1d-9a64-4e86-bf95-2af1de21d316\",\"faceRectangle\": { \"top\": 104, \"left\": 634, \"width\": 114, \"height\": 114 },\"faceAttributes\": { \"smile\": 0.074, \"gender\": \"male\", \"age\": 17.4, \"emotion\": { \"anger\": 0.003, \"contempt\": 0.003, \"disgust\": 0.001, \"fear\": 0.002, \"happiness\": 0.074, \"neutral\": 0.828, \"sadness\": 0.079, \"surprise\": 0.01 } }}]";
var result = JsonConvert.DeserializeObject<IList<RootObject>>(jsonString);
foreach ( var item in result )
Console.WriteLine( item.faceId );
}
}
// Generated by http://json2csharp.com
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public string gender { get; set; }
public double age { get; set; }
public Emotion emotion { get; set; }
}
public class RootObject
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
.net fiddle
If you want c# object from that JSON, this is how it is done:
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public string gender { get; set; }
public double age { get; set; }
public Emotion emotion { get; set; }
}
public class RootObject
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
var obj = JsonConvert.DeserializeObject<RootObject>(json);
Instead of giving you a C# class that maps to the JSON which you'll just copy blindly, I'll recommend that you generate such a class yourself.
Make sure you're running Visual Studio 2013 SP2 or higher.
Get a JSON document for your data. If it has optional fields, make sure the one you have is as full as possible.
In a new CS file in VS, choose Edit -> Paste Special -> Paste JSON as Classes.
This will create a C# class that corresponds to the JSON data. Now, you can use JSON.NET's JsonConvert.DeserializeObject() to create it
By using NewtonsoftJson library to parse data easily
Example
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata); //to parse data
foreach (var product in x) {
Messagebox.show(product.data.ToString());
}
You can convert Json to c# Class
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public string gender { get; set; }
public double age { get; set; }
public Emotion emotion { get; set; }
}
public class Example
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
Example Example_class =
Newtonsoft.Json.JsonConvert.DeserializeObject<Example>(json.ToString());
Please see this useful link Json to C# Class https://jsonutils.com/
Do you mean something like this? To deserialize to an object?
using System.Web.Script.Serialization;
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public string gender { get; set; }
public double age { get; set; }
public Emotion emotion { get; set; }
}
public class RootObject
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
return JsonConvert.DeserializeObject<RootObject>(jsonString);
I hope this will work
string Jsondata = "Your Json data";
public class Mainclass
{
public guid faceId;
IEnumerable<faceRectangle>
IEnumerable<faceAttributes>
}
public class faceRectangle
{
}
public class faceAttributes
{
}
Mainclass backdata = JsonConvert.DeserializeObject<Mainclass>(Jsondata , new DataConverter())

How to deserialize complex JSON string into Dataset in c#?

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

json feed as a datasource in SSIS

I am trying to deserialize a json feed from openweathermap.org api. After a lot of tries all, using script transformation task, I get is errors and even different ones. Based on the below classes generated by JSON, how I should write the for each statements (as class Weather and Main are part of class RootObject)?
I can get even XML data from api, instead of JSON. Would it be easier to implement the process?
I am not asking for the answer, just a push to start.
JSON:
{
"coord":{
"lon":-122.08,
"lat":37.39
},
"weather":[
{
"id":741,
"main":"Fog",
"description":"fog",
"icon":"50n"
}
],
"base":"stations",
"main":{
"temp":286.14,
"pressure":1022,
"humidity":82,
"temp_min":285.15,
"temp_max":287.15
},
"visibility":16093,
"wind":{
"speed":1.11,
"deg":13.5055
},
"clouds":{
"all":1
},
"dt":1479110160,
"sys":{
"type":1,
"id":397,
"message":0.1452,
"country":"US",
"sunrise":1479134859,
"sunset":1479171466
},
"id":5375480,
"name":"Mountain View",
"cod":200
}
Class:
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 Main
{
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 double 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; }
}
public class RootObject
{
public Coord coord { get; set; }
public List<Weather> weather { get; set; }
public string #base { get; set; }
public Main main { 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; }
}

Categories

Resources