Receiving json from a web server

Asked

Viewed 100 times

0

I am working with firebase, and I am searching the server data with Httpwebrequest, I was able to recover this data in the following way;

{"-L1od7ljenm8zhsps7ne":{"Age":"18 years","Name":"Lecturer","Telephone":"11 970705570"},"-L1odhpkmz_1zccfpzwf":{"Age":"10 years","Name":" ","Telephone":"98294792"},"-L1odmfc92yisdg4uxpu":{"Age":"30 years","Name":"Rune","Telephone":"98294792"}}

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 so I can manipulate both the data and the Keys.

If anyone can help, thank you.

  • Please explain a little more... to var text no longer has json content as string, something like { obj : value } ?

  • Hey man, this Json is weird, what does it mean ? L1od7ljenm8zhsps7ne

  • L1od7ljenm8zhsps7ne that is the key of the json file that is generated by firebase

  • I have the data as a string, I need to transform it into a list, in this string has data of 3 users, I need to separate the data and the Keys in list to be able to work with them

1 answer

0

If I may, I would like to point out a good way to solve this problem: Install the Newtonsoft package from Nuget: https://www.nuget.org/packages/Newtonsoft.Json/10.0.3

PM> Install-Package Newtonsoft.Json

Then, turn the received string into a dynamic list to work with:

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 Index, you can use (Jproperty)x). Path instead of (Jproperty)x. Could also create a dictionary to return key and value (beware of duplicated Dice).

After this point, I added a simple Writeline to present on the console, but then you will already have the object list you want. See below the whole code:

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

A imagem do resultado

See working in: https://dotnetfiddle.net/LjGMeG

  • Thank you so much for your help, but I’m having some problems here not recognizing Jtoken, nor this list.Select, I don’t know how to proceed further

Browser other questions tagged

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