String reading Json format for c#

Asked

Viewed 861 times

0

Hello, I’m having several weeks how to do to read the values of string below, I would like to read the value and on the sites is very confused in the search why the type json that I have begins with {{ and has an object in the middle of the string [{. would like to read the production field and media. I am not finding the correct code.

I have the string below:

{"d":{"media":12.108320606149539,"lote":"","Opcao":[{"__type":"Model","leitura":70,"producao":1579981660130}],"sinal":"Up"}}

grateful for the help.

  • 2

    This json does not work. Apparently it is not complete. It has an extra "{" at the beginning, which is never closed, and a comma where it should not. Where are you getting it from?

  • sorry, you were right. json was not correct.

  • follows the correct json: {"d":{"media":12.108320606149539,"lot":"","Option":[{"__type":"Model","reading":70,"production":1579981660130}],"signal":"Up"}}

3 answers

2

To catch the json {"d":{"media":12.108320606149539,"lote":"","Opcao":[{"__type":"Model","leitura":70,"producao":1579981660130}],"sinal":"Up"}} and turns it into object you can use the following code:

Obs: the [] indica array, you can read more here on reply

class Obj
{
    public D D { get; set; }
}

class D
{
    public string Media { get; set; }
    public string Lote { get; set; }
    public string Sinal { get; set; }
    public List<Opcao> Opcao { get; set; }
}

class Opcao
{
    public string __type { get; set; }
    public string Leitura { get; set; }
    public string Producao { get; set; }
}

Then, if your project doesn’t already have it, download the library Newtonsoft.Json (she is available via nuget)

and do the Deserializejson.

In the example below I saved it in a string

class Program
{
    static void Main(string[] args)
    {            
        string json = @"{'d':{'media':12.108320606149539,'lote':'','Opcao':[{'__type':'Model','leitura':70,'producao':1579981660130}],'sinal':'Up'}}";

        Obj myObj = new Obj();

        myObj = JsonConvert.DeserializeObject<Obj>(json);

        Console.WriteLine(myObj.D.Media);
    }
}

It is possible to see working on .NET Fiddle

1

You can use a dynamic as you already know the structure and what you want to find becomes much simpler without needing to map the whole structure of an object to get only the value of some attributes you know where they are.

string json = @"{ 'd':{ 'media':12.108320606149539,'lote':'','Opcao':[{'__type':'Model','leitura':70,'producao':1579981660130}],'sinal':'Up'}}";

dynamic objeto = Newtonsoft.Json.Linq.JObject.Parse(json);

//recuperando o valor de media 
var media = objeto.d.media.Value;

//veja que Opcao é um array de objetos, nesse caso pegamos primeira posição
var producao = objeto.d.Opcao[0].producao.Value;

0


Hello, all right?

Next, take your json and put it on this site: http://json2csharp.com/

It will generate the classes in c#, then you just do the deserialization.

var objetoRetornado = Newtonsoft.Json.Deserialize<classeGerada>(stringJson);
  • EXCELLENT, solved many problems. With the conversion of the classes saved me time creating. Still had a problem finding the amount of elements, this also solved. Thank you all for the collaboration.

Browser other questions tagged

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