Popular the properties of a class

2

A doubt. Before the service returned values in the following signature: Predicted / Made , this made it much easier. Now the subscription has changed to this:

{
        "CorIndicador":"VERMELHO",
        "DadosIndicador":"{\"Previsto\":25784.686452608872,\"Realizado\":95258.9557949728}",
        "TipoIndicador":1
    }

with the previous signature of the IndicatorItem class that was like this:

public string Nome { get; set; }
public decimal Valor { get; set; }

Now with the new signature, you have entered two new CorIndicator and TypeIndicator fields. How would I make the class popular?

public class IndicadorData : List<IndicadorItem>
{}

So I deserealize

var resp = JsonConvert.DeserializeObject<KpiResponse>(response.Content);

This is the Kpi class

[DataContract]
    public class KpiResponse
    {
        [DataMember]
        public TipoIndicador TipoIndicador { get; set; }
        [DataMember]
        public string CorIndicador { get; set; }
        [DataMember]
        public string DadosIndicador { get; set; }
    }

After deserializing in the kpi class, I do so to get the DataId

var fat = JsonConvert.DeserializeObject<FaturamentoResponse>(resp.DadosIndicador);

This is the class BillingResponse

public class FaturamentoResponse
    {
        public double Previsto { get; set; }
        public double Realizado { get; set; }
    }

That's exactly what I have. What should I do in this case to populate this class (IndicatorItem)? Just the way it is. What is the best way to do this?

EDIT1

I changed the IndicatorData class by adding the CorIndicator field and at the time of consuming the Service, in switch..case I do this:

var fat = JsonConvert.DeserializeObject<FaturamentoResponse>(resp.DadosIndicador);
                    res.CorIndicador = resp.CorIndicador;
                    res.Add(new IndicadorItem { Nome = "Realizado", Valor = Convert.ToDecimal(fat.Realizado) });
                    res.Add(new IndicadorItem { Nome = "Previsto", Valor = Convert.ToDecimal(fat.Previsto) });
    
asked by anonymous 12.02.2018 / 13:01

1 answer

1

An example class to load information from this return :

public class Rootobject
{
    public string CorIndicador { get; set; }
    public Dadosindicador DadosIndicador { get; set; }
    public int TipoIndicador { get; set; }
}

public class Dadosindicador
{
    public decimal Previsto { get; set; }
    public decimal Realizado { get; set; }
}

Because its Dadosindicador is an object with two fields of type decimal :

Rootobject root = JsonConvert.DeserializeObject<Rootobject>(json);

If you want to facilitate the creation of these classes you can use the following media:

Create a blank file, select the content by copying to memory and then in the .cs click menu to be created within that Edit :: Paste Especial :: Paste JSON As Classes file the classes for .cs of that Layout , example :

  

    
12.02.2018 / 15:01