Popular the properties of a class

Asked

Viewed 54 times

2

A question. Before the service returned values in the following signature: Planned/Accomplished, 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 the class Indicadoritem that was so:

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

now with the new signature, as entered two new fields Chorister and Tipoindicator. How I would do to popular the class?

public class IndicadorData : List<IndicadorItem>
{}

so I de-realize

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 deserealizar in the kpi class, I do so to catch the Data

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

that’s the class Billing response

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 popular this class(Indicadoritem)? The way it is does not work. Which way to do it?

EDIT1

I changed the Indicadordata class by adding the Index field and at the time of consuming the Service, switch..case i do it:

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) });
  • This Json is just that, or is it a list of that? if it’s a list put the example in the question!

  • @Virgilionovic, that’s just the json. It comes that way, it only varies the values, but that’s just.

  • You want an example to consume this json?

1 answer

1

An example class to load the return information :

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, your Dadosindicador is an object with two fields of the type decimal:

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

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

Create a file .cs blank, select the contents copying to memory and then in the menu Edit :: Paste Especial :: Paste JSON As Classes click to be created inside that file .cs the classes relating to Layout that json, example:

inserir a descrição da imagem aqui

  • It’s not two camps, actually I have one List<Indicadoritem> with Name(string) and Value(decimal), this class Indicadoritem. The Invoicing class yes, are two fields, but I only use it to deserialize the Data json.

  • Your Json @pnet is how you do when you propose a json layout to us... and I really don’t understand what you need, because as already said by Json That’s how it is. What you need?

  • Your Json are two properties yes @pnet ... is not a list no, at least what is in the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.