Receiving json from a web server

0

I'm working with firebase, and am fetching the data from the server with HttpWebRequest, I was able to retrieve this data as follows;

  

{"- L1OD7LjENM8ZHSpS7NE": {"Age": "18   years "," Name ":" vitor "," Telephone ":" 11   970705570 "}," - L1ODHPKmz_1zcCfpZwF ": {" Age ":" 10 years "," Name ":" joao   "," Phone ":" 9898294792 "}," - L1ODMFC92yisdG4UxPU ": {" Age ":" 30   years "," Name ":" bruno "," Phone ":" 9898294792 "}}

Using this code below;

HttpWebRequest pesquisar = (HttpWebRequest)WebRequest.CreateHttp(URL);
pesquisar.ContentType = "application/json: charset=utf-8";
HttpWebResponse pesquisar1 = pesquisar.GetResponse() as HttpWebResponse;
using (Stream pesquisarStream = pesquisar1.GetResponseStream())
{
    StreamReader reader = new StreamReader(pesquisarStream, Encoding.UTF8);

            var text = reader.ReadToEnd();

            richTextBox1.Text = text;
}

I would like to receive this data, directly in a json file and create a list to manipulate both the data and the keys.

If anyone can help, thank you.

    
asked by anonymous 30.12.2017 / 20:02

1 answer

0

If I may, I'd like to tell you a good way to solve this problem: Install the NewtonSoft package from Nuget: link

PM> Install-Package Newtonsoft.Json

Then, transform the received string into a dynamic list in order to work:

var listaDinamica = JsonConvert.DeserializeObject<JToken>(text);

After that, get the values of the elements, in order to remove the indices:

var lista = listaDinamica.Select(x => ((JProperty)x).Value.ToObject<Cliente>()).ToList();

At this point, if you need the Index, you can use the (JProperty) x) .Path instead of (JProperty) x) .Value. You could still create a dictionary to return key and value (careful with duplicate index).

After that point, I added a simple WriteLine to display on the console, but then you already have the list of objects you want. See the whole code below:

using System;
using Newtonsoft.Json;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace ConsoleApp1
{
    class Program
    {
        public class Cliente
        {
            public string Idade { get; set; }
            public string Telefone { get; set; }
            public string Nome { get; set; }
        }

        static void Main(string[] args)
        {

            var text = "{\"-L1OD7LjENM8ZHSpS7NE\":{\"Idade\":\"18 anos\",\"Nome\":\"vitor\",\"Telefone\":\"11 970705570\"},\"-L1ODHPKmz_1zcCfpZwF\":{\"Idade\":\"10 anos\",\"Nome\":\"joao \",\"Telefone\":\"9898294792\"},\"-L1ODMFC92yisdG4UxPU\":{\"Idade\":\"30 anos\",\"Nome\":\"bruno\",\"Telefone\":\"9898294792\"}}";

            var listaDinamica = JsonConvert.DeserializeObject<JToken>(text);
            var lista = listaDinamica.Select(x => ((JProperty)x).Value.ToObject<Cliente>()).ToList();

            foreach (var item in lista)
            {
                Console.WriteLine(item.Nome);
            }
        }
    }
}

Seeworkingat: link

    
03.01.2018 / 11:35