2017-10-23 3 views
0

Im VS2017でC#を使用しています。私は、データを取得する場所からJSONを持っており、この3つのクラス:不明なオブジェクトタイプを格納する

public class Azar 
{ 
    public int id_juego { get; set; } 
    public string timestamp { get; set; } 
    public int id_tipojuego { get; set; } 
    public string fecha { get; set; } 
    public int? sorteo { get; set; } 
    public object resultados { get; set; } 
    public string tipo_juego { get; set; } 
    public int tipo { get; set; } 
} 

public class ResultadoQuiniela 
{ 
    public string letras { get; set; } 
    public int[] numeros { get; set; } 
} 

public class ResultadoTelekino 
{ 
    public int sorteo { get; set; } 
    public int b1 { get; set; } 
    public int b2 { get; set; } 
    public int b3 { get; set; } 
    public int b4 { get; set; } 
    public int b5 { get; set; } 
    public int b6 { get; set; } 
    public int b7 { get; set; } 
    public int b8 { get; set; } 
    public int b9 { get; set; } 
    public int b10 { get; set; } 
    public int b11 { get; set; } 
    public int b12 { get; set; } 
    public int b13 { get; set; } 
    public int b14 { get; set; } 
    public int b15 { get; set; } 
    public int cat1 { get; set; } 
    public int cat2 { get; set; } 
    public int cat3 { get; set; } 
    public int cat4 { get; set; } 
    public int cat5 { get; set; } 
    public int cat6 { get; set; } 
    public int cat7 { get; set; } 
    public string prm1 { get; set; } 
    public string prm2 { get; set; } 
    public string prm3 { get; set; } 
    public string prm4 { get; set; } 
    public string prm5 { get; set; } 
    public string prm6 { get; set; } 
    public string prm7 { get; set; } 
    public string pozo { get; set; } 
    public string ext { get; set; } 
} 

Azar-> resultadosオブジェクトは2クラス、またはResultadoQuinielaまたはResultadoTelekinoのいずれかであることができます。私はjsonを解析してid_tipojuegoを見るまで、それらのどれがあるかわからない。私は私が行うことを知っていたら:

if(curAzar.id_tipojuego == 25) 
{ 
    ResultadoQuiniela resultados = (ResultadoQuiniela)curAzar.resultados; 
} 

をしかし、私はnullの結果を得ます。私はcurAzar(jsonから解析された結果である)が、すべての属性がresultadosオブジェクトに設定されていることを確認します。その情報を「ニュートラルな」方法で保存し、それを正しいオブジェクトにキャストするにはどうすればよいですか?それをオブジェクトとして宣言し、それをキャストすると、値はまったく格納されません。

EDIT:Newtonsoft.Json

+2

[JSONをC#の動的オブジェクトにデシリアライズしますか?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) –

+1

カスタム[JsonConverter ](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm)は、Json.netを使用している場合にこれを検出します。それをセットアップするには少しの労力がかかりますが、エンドユーザーにとっては非常に親密であるかもしれませんが、あなたのデータに基づいてどの種類のオブジェクトが入ってくるかを決めることができます – Icepickle

+0

JSONからオブジェクトを逆シリアル化するのにどうしますか? – Valerii

答えて

0

私は実用的なソリューションを見つけました: 私はresultadoフィールド(動的オブジェクト)を取得する場合、私はそれを文字列に変換し、その後、このように状況に応じて適切なクラスにそれをデシリアライズ:

JsonConvert.DeserializeObject<ResultadoQuiniela>(curAzar.resultado.ToString()); 

これは正常に動作し、エラーは発生しません(通常のキャストとしては実行できません)。しかし、もっと良い方法がある場合は、別の答えを待つつもりです。

0

を使用してJSONイムを解析するためのあなたにもそのJSONの直列化を制御していますか?あなたがそのJSONでの型情報を含めるようにシリアライザを取得した場合ので、

JsonSerializerSettings.TypeNameHandling 

に見れば。 Newtonsoft.Jsonのデシリアライザは自動的に処理します。 あなたはただの基本クラスResultadoを宣言します。そこから他の2つは継承され、次にあなたのAzarのpublicオブジェクトresultados {get;セット; }、

public Resultado resultado{ get; set; } 

またはあなたが期待している方

public Resultado[] resultados{ get; set; } 

を宣言します。

+0

もう少し説明できますか? –

+0

まず、JSONをどこから入手していますか?どのようにそれを作成し、それを作成するコードを変更することができますか? –

関連する問題