Popular class with json

Asked

Viewed 138 times

0

I got this json coming from my service

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

And I got that class

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

when I do that

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

to var Resp comes Corindicador only populated. Predicted and Realized is coming "0". I’m sure it’s because of the Dadosindicator. How I populate the properties Predicted and Realized.

EDIT1

looking at my briefcase, 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 she’s the one who should get the json, right?

EDIT2 I did it the way down and made that mistake

Unhandled Exception:

Newtonsoft.Json.Jsonserializationexception: Error Converting value "{"Predicted":55022.101316145665,"Realized":19938.330920384418}" to type 'Operativo.Models.Dadosresponse'. Path 'Dadosindicator', line 1, position 112. occurred

EDIT3

I switched to the class I had posted Dadosresponse and there the Data is string.

2 answers

2


You need to notice that actually, DadosIndicador is a string.

You need:

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

Here’s an example of how to do it in the last mentioned 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 on . NET Fiddle

2

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

So if your JSON structure is:

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

So 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); 

Browser other questions tagged

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