JsonConvert. SerializeObject () is used to implement json-type object conversion from jsonobject to java object.

Source: Internet
Author: User

JsonConvert. SerializeObject () is used to implement json-type object conversion from jsonobject to java object.

Currently, the project uses the idea of frontend and backend separation. Angular. JS is used for the frontend, And the backend uses the ABP framework. In the backend, we use WebAPI technology to provide json data to the frontend. Previously, I used MVC to write front-end code. I felt that the back-end was somewhat at the control of the front-end pace. Some small amounts of back-end code still needs to be written on the HTML page. This mode is used this time, the front-end does not need to write a little back-end C # code, but is only responsible for writing its own page. As for the back-end, it only needs to provide unified json format data, regardless of how the front-end displays it. This is the case. Let's take a look at how to json the backend data.

Convert back-end data to json

Assume that the front-end requires the following data format: The josn format provided by the backend is like this, which is used to convert the data format.

Then we define related classes to see how to implement such a format.

/// <Summary> /// the first class /// </summary> public class TreeView {[JsonProperty ("id")] public int Id {get; set ;} [JsonProperty ("text")] public string Text {get; set;} [JsonProperty ("children")] public IList <TreeChildrenView> Childrens {get; set ;}} /// <summary> /// the second largest class in the first class /// </summary> public class TreeChildrenView {[JsonProperty ("id")] public int Id {get; set;} [JsonProperty ("text")] public string Text {get; set;} [JsonProperty ("children")] public IList <Tree2ChildrenView> Childrens {get; set ;}} /// <summary> // The third category of the second major category. // </summary> public class Tree2ChildrenView {[JsonProperty ("id")] public int Id {get; set ;}[ JsonProperty ("text")] public string Text {get; set ;}}

If the backend needs to be jobized, Newtonsoft. Json must be referenced.

JsonConvert.SerializeObject();

Let's take a look at our code.

Static void Main (string [] args) {var treeView = new TreeView () {Id = 1, Text = "Shaanxi" ,}; var childrenTree = new TreeChildrenView () {Id = 2, Text = "Baoji City"}; var chchchtree = new Tree2ChildrenView () {Id = 3, Text = "Mei Xian"}; childrenTree. childrens = new List <Tree2ChildrenView> (); childrenTree. childrens. add (chchTree); treeView. childrens = new List <TreeChildrenView> (); treeView. childrens. add (childrenTree); string json = JsonConvert. serializeObject (treeView); Console. writeLine (json); Console. readLine ();}

We can see that only one sentence of conversion code is used. We can get the specific json data.

Label on the property: [JsonProperty ("id")]

Converts an uppercase Id to a lowercase Id in the json process. The rest means the same.

  • Instantiate List <>
 childrenTree.Childrens = new List<Tree2ChildrenView>();

Do I need to write this sentence every time. We can put it in the constructor:

    public class TreeView    {        public TreeView()        {            this.Childrens=new List<TreeChildrenView>();        }        [JsonProperty("id")]        public int Id { get; set; }        [JsonProperty("text")]        public string Text { get; set; }        [JsonProperty("children")]        public IList<TreeChildrenView> Childrens{ get; set; }    }

In this way, we can use it directly every time.

            childrenTree.Childrens.Add(chchTree);

You do not need to instantiate it because it is automatically instantiated when it is called by itself.

Related Article

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.