How does C # Generate JSON strings? (serialized objects)

Source: Internet
Author: User

Chapter One: How does C # get the JSON data back from HTTP? Chapter Two: How does C # parse JSON data? (Deserialization object) Chapter III: How does C # generate a JSON string? (serialized Object) Chapter Fourth: How C # generates a JSON string to submit to an interface (server)?

In the previous article we explained the serial number JSON data, this article we explain the deserialization of JSON data, that is, the entity class into JSON data, in fact, is very simple, in short, is to initialize the entity.

, we followed the first two code, adding a button to write the code in the button event. Let's look at the effect first.

And then the two are button events.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingNewtonsoft.json;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            //Our Interfaces            stringURL ="http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498"; //The interface is passed in, this HTTPUITLS class, interested can be studied, can also be used directly, without the tube how to achieve.             stringGetjson =httpuitls.get (URL);        MessageBox.Show (Getjson); }        Private voidButton2_Click (Objectsender, EventArgs e) {            //Our Interfaces            stringURL ="http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498"; //The interface is passed in, this HTTPUITLS class, interested can be studied, can also be used directly, without the tube how to achieve.             stringGetjson =httpuitls.get (URL); //This requires the introduction of the Newtonsoft.json DLL and using//It is OK to pass in our entity class and the JSON string that needs to be parsed. You can then use the data from the entity class. Root RT = Jsonconvert.deserializeobject<root>(Getjson); //This allows you to remove the values from the JSON dataMessageBox.Show ("com="+ rt.com +"\ r \ n"+"condition="+ Rt.condition +"\ r \ n"+"ischeck="+ Rt.ischeck +"\ r \ n"+"state="+ Rt.state +"\ r \ n"+"status="+rt.status); //since the public list<dataitem> data of this JSON string is a collection, we need to iterate through all the data in the collection             for(inti =0; i < Rt.data.Count; i++) {MessageBox.Show ("data="+ Rt.data[i].context +"\ r \ n"+ Rt.data[i].location +"\ r \ n"+ Rt.data[i].time +"\ r \ n"+rt.data[i].ftime); }        }        Private voidButton3_Click (Objectsender, EventArgs e) {            //generating a JSON string, actually assigning the entity entity we just wroteRoot RT =NewRoot (); RT.com="This is my assignment of COM"; Rt.condition="This is my assignment, condition."; Rt.ischeck="This is my assignment, Ischeck."; Rt.message="This is my assigned message."; Rt.state="This is my assignment, satate."; Rt.status="This is my assignment, Statcs."; List<DataItem> data =NewList<dataitem>(); DataItem DT=NewDataItem (); Dt.context="This is what I'm assigning."; Dt.time="This is my time of assignment."; Dt.ftime="This is my time of assignment."; Data.            ADD (DT); Rt.data=data; //we can put our initialized objects in.            stringJSON =Jsonconvert.serializeobject (RT);        MessageBox.Show (JSON); }    }}

How does C # Generate JSON strings? (serialized objects)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.