Reputation: 87
Json string
{"geonames":[{"continent":"EU","capital":"Andorra la Vella","languages":"ca","geonameId":3041565,"south":42.42849259876837,"isoAlpha3":"AND","north":42.65604389629997,"fipsCode":"AN","population":"84000","east":1.7865427778319827,"isoNumeric":"020","areaInSqKm":"468.0","countryCode":"AD","west":1.4071867141112762,"countryName":"Andorra","continentName":"Europe","currencyCode":"EUR"},{"continent":"AS","capital":"Abu Dhabi","languages":"ar-AE,fa,en,hi,ur","geonameId":290557,"south":22.633329391479492,"isoAlpha3":"ARE","north":26.08415985107422,"fipsCode":"AE","population":"4975593","east":56.38166046142578,"isoNumeric":"784","areaInSqKm":"82880.0","countryCode":"AE","west":51.58332824707031,"countryName":"United Arab Emirates","continentName":"Asia","currencyCode":"AED"},{"continent":"AS","capital":"Kabul","languages":"fa-AF,ps,uz-AF,tk","geonameId":1149361,"south":29.377472,"isoAlpha3":"AFG","north":38.483418,"fipsCode":"AF","population":"29121286","east":74.879448,"isoNumeric":"004","areaInSqKm":"647500.0","countryCode":"AF","west":60.478443,"countryName":"Afghanistan","continentName":"Asia","currencyCode":"AFN"},{"continent":"AF","capital":"Harare","languages":"en-ZW,sn,nr,nd","geonameId":878675,"south":-22.417738,"isoAlpha3":"ZWE","north":-15.608835,"fipsCode":"ZI","population":"13061000","east":33.056305,"isoNumeric":"716","areaInSqKm":"390580.0","countryCode":"ZW","west":25.237028,"countryName":"Zimbabwe","continentName":"Africa","currencyCode":"ZWL"}]}
How can I force Newtonsoft to parse the data?
Exception Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'App.Geonames' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
UPDATED
Solution
Controller
string GeoNamesCountry = "http://api.geonames.org/countryInfo?username=justlearntutors&type=json";
string GeoNamesCountryResult = "";
using (WebClient webclient = new WebClient())
{
GeoNamesCountryResult = webclient.DownloadString(GeoNamesCountry);
}
GeonamesObject geonamesObject = Newtonsoft.Json.JsonConvert.DeserializeObject<GeonamesObject>(GeoNamesCountryResult);
Model
public class GeonamesObject
{
public GeonamesCountry[] geonames { get; set; }
}
public class GeonamesCountry
{
public string countryCode { get; set; }
public string countryName { get; set; }
public string isoNumeric { get; set; }
public string isoAlpha3 { get; set; }
public string fipsCode { get; set; }
public string continent { get; set; }
public string continentName { get; set; }
public string capital { get; set; }
public string areaInSqKm { get; set; }
public string population { get; set; }
public string currencyCode { get; set; }
public string languages { get; set; }
public int geonameId { get; set; }
public string west { get; set; }
public string north { get; set; }
public string east { get; set; }
public string south { get; set; }
public string postalCodeFormat { get; set; }
}
Upvotes: 0
Views: 1427
Reputation: 7213
You can use visual studio to auto-generate classes based your json string. Copy your json string, go
Edit -> Paste Special -> Paste JSON As Classes (Note: Your Json string needs to be valid)
then it will create classes for you (note: Use JsonProperty
attribute, to to serialize/desrialize the member with specified name)
public class Rootobject
{
[JsonProperty("geonames")]
public Geoname[] Geonames { get; set; }
}
public class Geoname
{
[JsonProperty("continent")]
public string Continent { get; set; }
[JsonProperty("capital")]
public string Capital { get; set; }
[JsonProperty("languages")]
public string Languages { get; set; }
[JsonProperty("geonameId")]
public int GeonameId { get; set; }
[JsonProperty("south")]
public float South { get; set; }
[JsonProperty("isoAlpha3")]
public string IsoAlpha3 { get; set; }
[JsonProperty("north")]
public float North { get; set; }
[JsonProperty("fipsCode")]
public string FipsCode { get; set; }
[JsonProperty("population")]
public string Population { get; set; }
[JsonProperty("east")]
public float East { get; set; }
[JsonProperty("isoNumeric")]
public string IsoNumeric { get; set; }
[JsonProperty("areaInSqKm")]
public string AreaInSqKm { get; set; }
[JsonProperty("countryCode")]
public string CountryCode { get; set; }
[JsonProperty("west")]
public float West { get; set; }
[JsonProperty("countryName")]
public string CountryName { get; set; }
[JsonProperty("continentName")]
public string ContinentName { get; set; }
[JsonProperty("currencyCode")]
public string CurrencyCode { get; set; }
}
then deserialize it:
var result = JsonConvert.DeserializeObject<Rootobject>(json_string);
Upvotes: 1
Reputation: 253
kindly check this things should work
GeonamesObject geonamesObject = new GeonamesObject();
geonamesObject.geonames =
Newtonsoft.Json.JsonConvert.DeserializeObject<List<Geonames>>(GeoNamesCountryResult);
Upvotes: 0