JSON serialization and deserialization in desktop application

Asked

Viewed 462 times

3

{"result":[[{"code reduced":"019076-3","Stock":200,"Price":900.77,"Price":225.19,"Description":" ASUS AMD RADEON R5 VIDEO CARD 230 1GB DDR3 64BITS R5230-SL-1GD3-L *IMP","Group":"VIDEO PLATE","Brand":"ASUS","Origin":6,"NCM":"8473.30.43","EAN":"0000000000000","Weighing":0,"Width":0,"Height":0,"Depth":0,"Warranty":"12 Months","Aliqipi":0,"Aliqicms":12,"Unit":"PC","Multiplovend":1,"Link":"https://www2.meusite.com.br/localnew/_img/fotos_products/019076-3.jpg","Partnumber":"R5230-SL-1GD3-L"}]]}

I’m Sending the Code I’m Developing..

    private void ButtonConvert_Click(object sender, EventArgs e)
    {
        string fileContents;
        WebRequest request = WebRequest.Create(textBox1.Text);

        WebResponse response = request.GetResponse();
        Stream data = response.GetResponseStream();
        string html = String.Empty;
        using (StreamReader sr = new StreamReader(data))
        {
            fileContents = sr.ReadToEnd();
        }
        richTextBox1.Text = fileContents;
        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
        Result paging = jsonSerializer.Deserialize<Result>(fileContents);
        textBox2.Text = textBox2.Text + paging.Descricao;

        for (int i = 0; i < 50; i++)
        {

            //richTextBox1.Text = richTextBox1.Text + paging.results[i].id + " ";
            //richTextBox1.Text = richTextBox1.Text + paging.results[i].buyer.id + " ";
            //richTextBox1.Text = richTextBox1.Text + paging.results[i].buyer.first_name + " ";



        }

    }
    public class Result
    {
        public string CodigoReduzido { get; set; }
        public int Estoque { get; set; }
        public double Preco { get; set; }
        public double PrecoST { get; set; }
        public string Descricao { get; set; }
        public string Grupo { get; set; }
        public string Marca { get; set; }
        public int Origem { get; set; }
        public string NCM { get; set; }
        public string EAN { get; set; }
        public double PesoBruto { get; set; }
        public int Largura { get; set; }
        public int Altura { get; set; }
        public int Profundidade { get; set; }
        public string Garantia { get; set; }
        public int AliqIPI { get; set; }
        public int AliqICMS { get; set; }
        public string Unidade { get; set; }
        public int MultiploVenda { get; set; }
        public string Link { get; set; }
        public string PartNumber { get; set; }
    }

    public class RootObject
    {
        public List<Result> result { get; set; }
    }

Using json2csharp generate c# classes from json

    public class RootObject
    {
         public List<List<>> result { get; set; }
    }

But I couldn’t use it that way.. Now I think I’ve got a better explanation

I appreciate the help of friends...

  • Click [Edit] and put your deserialization code there. I think it’s working, the problem is that you have to access these values as an array within another to get the values, that’s all.

  • Using Visual Studio itself he showed me that I would have to use to access the array in the following way paging[0]. [0] I believe that my problem is in the classes part of the JSON structure.... how to do..

3 answers

3

Natively, you can use the class System.Web.Script.Serialization.JavaScriptSerializer.

new JavaScriptSerializer().Serialize(obj);

Serialize the object obj.

new JavaScriptSerializer().Deserialize<T>(objStr);

Plays the opposite role by deserializing the string objStr for the guy T.

  • Thanks for the help so far.. I already made a part, but I still can not unravel this maledito rsrsr

  • Solved both ways, natively and JSON NET, Thanks for the help !!!

1

The JSON.NET package serializes an object for you:

https://www.nuget.org/packages/newtonsoft.json/

Thus:

string json = JsonConvert.SerializeObject(produto);

And deserializing:

var produto = JsonConvert.DeserializeObject<Produto>(jsonString);

The complete documentation is here.

  • Thanks for the help... I’m trying to use the native form.. first.. The fight continues... thanks

  • 1

    Solved both ways, natively and JSON NET, Thanks for the help !!!

0


Solved Problem in Two Ways

    public class RootObject
    {
        public List<List<Result>> result { get; set; }
    }

    public class Result
    {
        public string CodigoReduzido { get; set; }
        public int Estoque { get; set; }
        public double Preco { get; set; }
        public double PrecoST { get; set; }
        public string Descricao { get; set; }
        public string Grupo { get; set; }
        public string Marca { get; set; }
        public int Origem { get; set; }
        public string NCM { get; set; }
        public string EAN { get; set; }
        public double PesoBruto { get; set; }
        public int Largura { get; set; }
        public int Altura { get; set; }
        public int Profundidade { get; set; }
        public string Garantia { get; set; }
        public int AliqIPI { get; set; }
        public int AliqICMS { get; set; }
        public string Unidade { get; set; }
        public int MultiploVenda { get; set; }
        public string Link { get; set; }
        public string PartNumber { get; set; }
    }

Solved the problem of the Following Way Using JSON.NET

    private void Json2_Click(object sender, EventArgs e)
    {
        try
        {
            string fileContents;
            WebRequest request = WebRequest.Create(textBox1.Text);

            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();
            string html = String.Empty;
            using (StreamReader sr = new StreamReader(data))
            {
                fileContents = sr.ReadToEnd();
            }
            richTextBox1.Text = fileContents;

            JsonTextReader reader = new JsonTextReader(new StringReader(fileContents));
            RootObject rootobject = JsonConvert.DeserializeObject<RootObject>(fileContents);
            textBox2.Text = "";
            for (int i = 0; i < 1; i++)
            {
               for (int j = 0; j < rootobject.result[i].Count() ; j++)
                {
                    textBox2.Text = textBox2.Text + rootobject.result[i][j].CodigoReduzido + "   ";
                    textBox2.Text = textBox2.Text + "  " + rootobject.result[i][j].Descricao + "\r\n";
                    */* aqui voce coloca o restante dos seus campos */*
                }
            }
        }
        catch (Exception ex)
        {
            textBox3.Text = ex.Message;
        }
    }

Solution using Javascriptserializer Native Mode

    private void ButtonConvert_Click(object sender, EventArgs e)
    {
        try
        {
            string fileContents;
            WebRequest request = WebRequest.Create(textBox1.Text);
            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();
            string html = String.Empty;
            using (StreamReader sr = new StreamReader(data))
            {
                fileContents = sr.ReadToEnd();
            }
            richTextBox1.Text = fileContents;
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();

            RootObject rootobject = new JavaScriptSerializer().Deserialize<RootObject>(fileContents);
            textBox2.Text = "";
            for (int i = 0; i < 1; i++)
            {
                for (int j = 0; j < rootobject.result[i].Count() ; j++)
                {
                    textBox2.Text = textBox2.Text + rootobject.result[i][j].CodigoReduzido + "   ";
                    */* aqui voce coloca o restante dos seus campos */*
                }
            }
        }
        catch (Exception ex)
        {
            label1.Text = ex.Message;
        }

    }

Browser other questions tagged

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