. Net object class and json conversion (. net built-in class library implementation), json.net

Source: Internet
Author: User

. Net object class and json conversion (. net built-in class library implementation), json.net

Important points.

1. Reference added during jsonhelp writing. System. Runtime. Serialization. Json;

2. The object class must be declared as public.

Jsonhelp code:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. runtime. serialization. json; using System. IO; namespace JsonTest {class JsonHelp {public JsonHelp () {// TODO: add constructor logic here // <summary> // serialize the object to a JSON string /// </summary> /// <typeparam name = "T"> Object type </typeparam> /// <param name = "obj"> Object entity </param> /// <returns> JSON string </returns> public static string GetJson <T> (T obj) {// remember to add reference System. serviceModel. web/*** if you do not add the above reference, System. runtime. serialization. json; Json is the bytes of the Oh **/DataContractJsonSerializer json = new DataContractJsonSerializer (typeof (T); using (MemoryStream MS = new MemoryStream () {json. writeObject (MS, obj); string szJson = Encoding. UTF8.GetString (ms. toArray (); return szJson ;}} /// <summary> /// restore the JSON string to an object // </summary> /// <typeparam name = "T"> Object Type </typeparam>/ // <param name = "szJson"> JSON string </param> // <returns> Object entity </returns> public static T ParseFormJson <T> (string szJson) {T obj = Activator. createInstance <T> (); using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (szJson) {DataContractJsonSerializer dcj = new DataContractJsonSerializer (typeof (T); return (T) dcj. readObject (MS );}}}}

Entity code

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace JsonTest{  public  class testData    {        public testData()    {    }    public int Id { get; set; }    public string Name { get; set; }    public string Sex { get; set; }    }}

Console Application Test code

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace JsonTest {class Program {static void Main (string [] args) {// convert the object class to json testData t1 = new testData (); t1.Id = 1; t1.Name = "001 name"; t1.Sex = "male"; testData t2 = new testData (); t2.Id = 2; t2.Name = "002 name"; t2.Sex = "male "; testData t3 = new testData (); t3.Id = 3; t3.Name = "003 name"; t3.Sex = "male"; List <testData> tlist = new List <testData> (); tlist. add (t1); tlist. add (t2); tlist. add (t3); Console. writeLine (JsonHelp. getJson <List <testData> (tlist); // Console. readKey (); // List of object classes converted from json to <testData> tl = JsonHelp. parseFormJson <List <testData> (JsonHelp. getJson <List <testData> (tlist); Console. writeLine (tl. count); Console. writeLine (tl [0]. name); Console. readKey ();}}}

When searching for information, I found my blog posts.

Http://www.cnblogs.com/jinho/archive/2010/04/18/1715044.html

Http://blog.csdn.net/wang_cel/article/details/44587201

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.