I try to generate C# class with JSON datas. This datas are on this site
Method 1 : I used this online builder builder online
Method 2 : I used special paste to JSON into Visual Studio 2015 (like explain here)
Conclusion: not same result! Why?
Result with online site :
public class Translations
{
public string de { get; set; }
public string es { get; set; }
public string fr { get; set; }
public string ja { get; set; }
public string it { get; set; }
}
public class RootObject
{
public string name { get; set; }
public List<string> topLevelDomain { get; set; }
public string alpha2Code { get; set; }
public string alpha3Code { get; set; }
public List<object> callingCodes { get; set; }
public string capital { get; set; }
public List<object> altSpellings { get; set; }
public string relevance { get; set; }
public string region { get; set; }
public string subregion { get; set; }
public int population { get; set; }
public List<object> latlng { get; set; }
public string demonym { get; set; }
public double? area { get; set; }
public double? gini { get; set; }
public List<string> timezones { get; set; }
public List<object> borders { get; set; }
public string nativeName { get; set; }
public string numericCode { get; set; }
public List<string> currencies { get; set; }
public List<object> languages { get; set; }
public Translations translations { get; set; }
}
Result with special paste of Visual Studio :
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string name { get; set; }
public string[] topLevelDomain { get; set; }
public string alpha2Code { get; set; }
public string alpha3Code { get; set; }
public string[] callingCodes { get; set; }
public string capital { get; set; }
public string[] altSpellings { get; set; }
public string relevance { get; set; }
public string region { get; set; }
public string subregion { get; set; }
public int population { get; set; }
public float?[] latlng { get; set; }
public string demonym { get; set; }
public float? area { get; set; }
public float? gini { get; set; }
public string[] timezones { get; set; }
public string[] borders { get; set; }
public string nativeName { get; set; }
public string numericCode { get; set; }
public string[] currencies { get; set; }
public string[] languages { get; set; }
public Translations translations { get; set; }
}
public class Translations
{
public string de { get; set; }
public string es { get; set; }
public string fr { get; set; }
public string ja { get; set; }
public string it { get; set; }
}
Worse! The deserialisation with VS code does'nt work!
Code for deserialise :
string url = @"http://restcountries.eu/rest/v1";
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable<Rootobject>));
WebClient syncClient = new WebClient();
string content = syncClient.DownloadString(url);
using (MemoryStream memo = new MemoryStream(Encoding.Unicode.GetBytes(content)))
{
IEnumerable<Rootobject> countries = (IEnumerable<Rootobject>)serializer.ReadObject(memo);
int i = countries.Count();
}
Console.Read();
Have you an idea of this difference? Bug VS?