Popular class with json

0

I have this json coming from my service

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

And I have this class

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

When I do this

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

a var resp comes CorIndicator only populated. Predicted and Realized is coming "0". I'm sure it's because of the DataIndicator. How do I populate the Predicted and Realized properties.

EDIT1

Looking good at my folder, I saw this class

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

I think it's the one json should get, right?

EDIT2 I did the form below and gave this error

  

Unhandled Exception:

     

Newtonsoft.Json.JsonSerializationException: Error converting value   "{" Forecasted ": 55022.101316145665," Performed ": 19938.330920384418}" to   type 'Operational.Models.DadosResponse'. Path 'DataDisplay', line   1, position 112.

EDIT3

I changed the class I had posted to DataResponse and the DataIndicator is string .

    
asked by anonymous 08.02.2018 / 19:02

2 answers

2

You should note that, in fact, DadosIndicador is a string .

You need:

  • set up JSON;
  • or customize the deserializer;
  • or use the property as string ;
  • or deserialize it separately.

Here's an example of how to do the last quoted way

using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        var response = new 
        {
            Content = @"{""CorIndicador"": ""VERMELHO"", ""DadosIndicador"": ""{\""Previsto\"":25784.686452608872, \""Realizado\"":95258.9557949728} "",""TipoIndicador"":1}"
        };

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

        Console.WriteLine(resp.DadosIndicador);

        var dados = JsonConvert.DeserializeObject<DadosResponse>(resp.DadosIndicador);

        Console.WriteLine(dados.Previsto);
        Console.WriteLine(dados.Realizado);
    }
}

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

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

See working in .NET Fiddle

    
08.02.2018 / 19:15
2

For you to deserialize an object successfully you must have the corresponding classes in the model.

So if your JSON framework is:

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

Then your model class should be:

public class FaturamentoResponse
{
    public string CorIndicador { get; set; }
    public DadosResponse DadosIndicador { get; set; }
    public int TipoIndicador { get; set; }
}

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

And to deserialize you can still use:

var resp = JsonConvert.DeserializeObject<FaturamentoResponse>(response.Content); 
    
08.02.2018 / 19:21