0

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?

Esperento57
  • 16,521
  • 3
  • 39
  • 45

1 Answers1

1

In the second example, Visual Studio actually wraps the "root object" with yet another root object that contains an array of Class1.

Since the payload data structure root is an array, not an object, this would appear to be a bug, provided you used the correct payload to generate the structure.

As a result, just replace Rootobject references with Class1.

string url = @"http://restcountries.eu/rest/v1";
DataContractJsonSerializer serializer = 
    new DataContractJsonSerializer(typeof(IEnumerable<Class1>));
WebClient syncClient = new WebClient();
string content = syncClient.DownloadString(url);

using (MemoryStream memo = new MemoryStream(Encoding.Unicode.GetBytes(content)))
{
   IEnumerable<Class1> countries = (IEnumerable<Class1>)serializer.ReadObject(memo);
   int i = countries.Count();
}

Console.Read();

As an aside, you should really switch to a more modern serializer such as Newtonsoft JSON.NET.

David L
  • 32,885
  • 8
  • 62
  • 93
  • ty for your answer ;) i have try your code but deserialise are empty – Esperento57 Jun 02 '17 at 20:22
  • This is what I thought there was a bug listed Visual Studio, thanks for this confirmation. I tell myself that I can not trust VS completely to generate my classes from data in JSON format. As for using the Class1 class for deserialization, I had found the same thing as you. :) I'm going to look at Newtonsoft JSON.NET. Thank you. – Esperento57 Jun 03 '17 at 07:53