Common techniques used in C # JSON (ii)

Source: Internet
Author: User

JSON in PHP in a sentence json_encode can get

In C # We can also easily get

Library of classes used: Newtonsoft.Json.dll

Entity class:

Class Cat {public string A {get; set;} public string B {get; set;}}

by Jsonconvert:serializeobject

Cat cat = new Cat () {A = "123", B = "245"};

var json = Jsonconvert.serializeobject (cat);

The result is: {"A": "123", "B": "245"}

Similarly, if we have a field structure and {"A": "123", "B": "245"} The same class (contains A, B on the line), for example, say:

Class Dog {public string A {get, set;} public string B {get; set;} public string C {get; set;}}

We can also deserialize objects of this class;

var dog = jsonconvert.deserializeobject<dog> ("{\" a\ ": \" 123\ ", \" b\ ": \" 245\ "}");

Result: Dog. A:123

Dog. B:123

Dog. C:null

Common techniques used in C # JSON (ii)

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.