2017-06-02 5 views
0

私はJSONデータでC#クラスを生成しようとしています。この件のデータは、このsite違いJSONデシリアライズ 'オンラインサイト'と 'Visual Studioに貼り付ける'

方法1である:同じではありません結果:私は、Visual Studio 2015にJSONに特殊な糊を使用

まとめ(のようなhereを説明):私はbuilder online

方法2このオンラインビルダーを使用しました!どうして?オンラインサイトで

結果:Visual Studioの特殊ペーストで

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; } 
} 

結果:

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; } 
} 

さらに悪いです! VSコードでのデシリアライゼーションは機能しません!

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(); 

はあなたにこの違いのアイデアをお持ちですか?バグVS?

答えて

1

2番目の例では、Visual Studioは実際には「ルートオブジェクト」をClass1の配列を含むさらに別のルートオブジェクトでラップします。

構造体を生成するために正しいペイロードを使用していれば、ペイロードデータ構造体ルートはオブジェクトではなく配列であるため、バグのように見えます。

Rootobjectの参照を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(); 

さて、実際にはNewtonsoft JSON.NETなどの最新のシリアライザに切り替える必要があります。

+0

あなたの答えを試してください;)あなたのコードを試しましたが、デシリアライズは空です。 – Esperento57

+0

@ Esperento57アップデートをご覧ください。 –

+0

これは、この確認のために、Visual Studioにバグが登録されていると思いました。私は、JSON形式のデータからクラスを完全に生成するためにVSを完全に信頼することはできないと自分自身に伝えます。デシリアライズにClass1クラスを使用する場合、私はあなたと同じことを見つけました。 :)私はNewtonsoft JSON.NETを見ていきます。ありがとうございました。 – Esperento57

関連する問題