How to get a single value of a Json [duplicate]

0

I have my response that comes in json I get it in the following way:

StreamReader sr = new StreamReader(newStream);
                    String json = sr.ReadToEnd();

With a WriteLine it shows me:

[{"id":"1","correo":"[email protected]","clave":"123456","numero":"+1 8XX-307-7455"}]

The problem is that I only want the numero how can I get it?

    
asked by DoubleM 08.02.2018 в 03:49
source

1 answer

3

What you can try is to convert your response into a JSON object in the following way

StreamReader sr = new StreamReader(newStream);
String json = sr.ReadToEnd();

dynamic jsonObj = JsonConvert.DeserializeObject(json);

And once you have the object you can access one of its attributes as follows:

    // el [0] por que es un JsonArray y define la posición en cual buscar.
    var numero = jsonObj[0]["numero"].ToString();
    Console.WriteLine(numero);  

Look, I've tried this compiler online: link

Anyway, I'll leave you the code you use to prove it:

using System;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        String json = ("[{\"id\":\"1\",\"correo\":\"[email protected]\",\"clave\":\"123456\",\"numero\":\"+1 8XX-307-7455\"}]");
        dynamic jsonObj = JsonConvert.DeserializeObject(json);
        Console.WriteLine(jsonObj);
        var numero = jsonObj[0]["numero"].ToString();
        Console.WriteLine(numero);            
    }
}
    
answered by 08.02.2018 в 04:02