Serialization and Deserialization JSON in desktop application

3
  "Price": 200, "Price": 900.77, "PrecoST": 225.19, "Description": "   ASUS AMD RADEON R5 230 VIDEO CARD 1GB DDR3 64BITS R5230-SL-1GD3-L   "IMP", "Group": "VIDEO PLATE", "Mark": "ASUS", "Source": 6, "NCM": "8473.30.43", "EAN": "0000000000000", " 0, "Width": 0, "Height": 0, "Depth": 0, "Warranty": "12   Months "," AliqIPI ": 0," AliqICMS ": 12," Unit ":" PC "," Multi-Sale ": 1," Link ":" https://www2.myusite.com/localnew/_img/ photos_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 could not use it that way .. Now I think I've been able to explain better.

I appreciate the help of friends ...

    
asked by anonymous 16.09.2014 / 21:30

3 answers

0

Solved Two Way Problem

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

    }
    
18.09.2014 / 19:09
3

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

new JavaScriptSerializer().Serialize(obj);

Serializes the object obj .

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

Do the opposite, deserializing the string objStr to type T .

    
16.09.2014 / 21:57
1

The JSON.NET package serializes an object for you:

  

link

So:

string json = JsonConvert.SerializeObject(produto);

And deserializing:

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

The complete documentation is here .

    
16.09.2014 / 21:53