Reputation: 240
I am consuming an API which returns the following JSON
{"coord":{"lon":-1.38,"lat":54.91},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":285.83,"pressure":1013,"humidity":76,"temp_min":285.15,"temp_max":286.15},"visibility":10000,"wind":{"speed":5.1,"deg":240},"clouds":{"all":20},"dt":1536913200,"sys":{"type":1,"id":5104,"message":0.0049,"country":"GB","sunrise":1536903391,"sunset":1536949457},"id":2636531,"name":"Sunderland","cod":200}
I call the API and return the JSON as a string (strJson), from strJson I use the following code to deserialize the JSON and return the values.
JsonResponse res = JsonConvert.DeserializeObject<JsonResponse>(strJson);
I am able to return the items that reside within "weather" by using the following inside my JsonResponse class (I'm only returning "main" at the moment, which works successfully;
class JsonResponse
{
public object coord { get; set; }
public IList<Weather> weather { get; set; }
}
}
class Weather
{
public string main { get; set; }
}
I have tried all sorts to return the data that resides within "coord" and "sys" but i keep getting the error "Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly".
I am able to return into into an "object" using
public object coord {get; set;}
however this isn't effective as I'm still unable to quickly extract the variables lon & Lat as well as their values.
Any help would be appreciated as I've asked a similar question before, however the answers I've found stop me from being able to extract the values from "weather" so I'm stumped.
Upvotes: 1
Views: 59
Reputation:
You need to use dynamic for the object see here: Deserialize json object into dynamic object using Json.net
I can give a code example if you like, but there is one on the page.
Upvotes: 0
Reputation: 4515
What's wrong with this?
class JsonResponse
{
public CoordObject coord {get;set;}
}
class CoordObject
{
public double lon {get;set;}
public double lat {get;set;}
}
You should then be able to access res.coord.lon
and res.coord.lat
.
With a quick search "json detect class", I found this helpful site: json2csharp which generated the following classes for your JSON:
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 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; }
}
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; }
}
Upvotes: 3