Go C # JSON-formatted strings are read into the class

Source: Internet
Author: User

Tag: Pac read object string DLL Add Reference upload nbsp constructor

Reading a JSON-formatted string into a class

In this example, a JSON-formatted string JSON is created to read its contents into the person class

Running this code requires adding a reference to the dynamic library Newtonsoft.json

Program code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;//need to reference Newtonsoft.Json.dllusingNewtonsoft.json;namespacejsontest{classProgram {/// <summary>        ///Personnel category/// </summary>         Public classPerson { Public stringName//name             Public intAge//Age             Public BOOLSex_is_male;//Sex             Public structPartner//Partner            {                 Public stringPartner_name;//Partner name                 Public intPartner_age;//Partner Age                 Public BOOLPartner_sex_is_male;//Partner Sex            }             PublicPartner Partner;  Public string[] achievement;//Achievements//constructor Function             PublicPerson () {name=""; Age=0; Sex_is_male=true; Partner.partner_name=""; Partner.partner_age=0; Partner.partner_sex_is_male=true; Achievement=NULL; }        }        Static voidMain (string[] args) {            stringJSON =string. Concat ("{",                "\ "name\": \ "Tsybius\",",                "\ "age\":",                "\ "Sex_is_male\": true,",                "\ "Partner\":",                "    {",                "\ "partner_name\": \ "Galatea\",",                "\ "partner_age\":",                "\ "Partner_sex_is_male\": false",                "    },",                "\ "achievement\": [\ "ach1\", \ "ach2\", \ "ach3\"]",                "}"            ); //assigning content to the person classPerson p = jsonconvert.deserializeobject<person>(JSON); Console.WriteLine ("Name:"+p.name); Console.WriteLine ("Age :"+p.age); Console.WriteLine ("Sex_is_male:"+P.sex_is_male); Console.WriteLine ("Partner_name:"+p.partner.partner_name); Console.WriteLine ("Partner_age:"+p.partner.partner_age); Console.WriteLine ("Partner_sex_is_male:"+P.partner.partner_sex_is_male); Console.Write ("Achievement:"); foreach(stringAchvinchp.achievement) {Console.Write (ACHV+" ");            } Console.WriteLine ();        Console.ReadLine (); }    }}

Operation Result:

END

Go C # JSON-formatted strings are read into the class

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.