C # objects and JSON strings are converted to each other

Source: Internet
Author: User

DoNet2.0 need the help of Newtonsoft.Json.dll

1 Code2 3 usingSystem;4 usingSystem.IO;5 usingSystem.Text;6 usingNewtonsoft.json;7 8 namespaceOfflineacceptcontrol.uctools9 {Ten      Public classJsontools One     { A         //generating a JSON string from an object information -          Public Static stringObjecttojson (Objectobj) -         { the            returnjavascriptconvert.serializeobject (obj); -         } -         //generating object information from a JSON string -          Public Static ObjectJsontoobject (stringJsonstring,Objectobj) +         { -            returnjavascriptconvert.deserializeobject (jsonstring, obj. GetType ()); +         } A     } at}

Donet3.5 comes with DLL processing JSON string

Note reference: System.runtime.serialization,system.servicemodel.web

1 Code2 3 usingSystem;4 usingSystem.Collections.Generic;5 usingSystem.IO;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Runtime.Serialization;9 usingSystem.Runtime.Serialization.Json;Ten  One namespaceCrjiiofflineaccept.crjiitools A { -      Public classJsontools -     { the         //generating a JSON string from an object information -          Public Static stringObjecttojson (Objectobj) -         { -DataContractJsonSerializer serializer =NewDataContractJsonSerializer (obj. GetType ()); +MemoryStream stream =NewMemoryStream (); - Serializer. WriteObject (stream, obj); +            byte[] Databytes =New byte[Stream. Length]; AStream. Position =0; atStream. Read (Databytes,0, (int) stream. Length); -            returnEncoding.UTF8.GetString (databytes); -         } -         //generating object information from a JSON string -          Public Static ObjectJsontoobject (stringJsonstring,Objectobj) -         { inDataContractJsonSerializer serializer =NewDataContractJsonSerializer (obj. GetType ()); -MemoryStream Mstream =NewMemoryStream (Encoding.UTF8.GetBytes (jsonstring)); to            returnSerializer. ReadObject (mstream); +         } -     } the}

C # objects and JSON strings are converted to each other

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.