0

Im using C# in VS2017. I have a json from where I get data, and this 3 classes:

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

The Azar->resultados object can be any of the 2 classes, or ResultadoQuiniela or ResultadoTelekino. I don't know which of them is until I parse the json and see the id_tipojuego. Once I know that I do:

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

But I get null results. I see that curAzar (is the result parsed from the json) was every attribute set BUT the resultados object. How can I store that information in a "neutral" way and then cast it to the right object? Declaring it as object and then cast it it doesn't store the value at all.

EDIT: For parsing the json Im using Newtonsoft.Json

David TG
  • 85
  • 3
  • 10
  • 33
  • 2
    Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Samir Aguiar Oct 23 '17 at 23:21
  • 1
    You could write a custom [JsonConverter](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) that finds this out for you if you are using Json.net. It takes a bit of work to set it up, and it can be very intrasparent to end users, but you could decide based on your data which kind of object is incoming – Icepickle Oct 23 '17 at 23:21
  • What do you use to deserialize objects from JSON? – Valerii Oct 23 '17 at 23:34
  • For deserializing the json Im using Newtonsoft.Json – David TG Oct 24 '17 at 00:03
  • @SamirAguiar is not the same question. He wants to serialize the whole json as a dynamic type. I have a Azar type, but one of it fields is a dynamic type object. – David TG Oct 24 '17 at 14:16

2 Answers2

0

Do you have control over serialization of that json as well? If so look into

JsonSerializerSettings.TypeNameHandling 

If you get serialiser to include type information in that json. The Newtonsoft.Json's deserializer will just handle it for you automagically. You just declare a base class Resultado from which the other 2 inherit and then in your Azar insted of public object resultados { get; set; }, declare

public Resultado resultado{ get; set; }

or

public Resultado[] resultados{ get; set; }

whichever you are expecting.

ILIA BROUDNO
  • 1,539
  • 17
  • 24
0

I found a working solution: When I get the resultado field (a dynamic object) I transform it to string and then deserialize it to the correct class according to a condition in this way:

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

This works and gives no errors (as a regular cast, that can't be done). But I'll wait to another answer in case there's a better way to do it.

David TG
  • 85
  • 3
  • 10
  • 33