. How to parse JSON under net

Source: Internet
Author: User

. Several common parsing JSON methods under net
Main class name Space Limit built-in LINQ support
DataContractJsonSerializer System.Runtime.Serialization.Json General Whether
JavaScriptSerializer System.Web.Script.Serialization Can only be used in a Web environment Whether
Jsonarray, Jsonobject, Jsonvalue System.json Can only be used in Silverlight Is
JsonConvert, Jarray, Jobject, Jvalue, Jproperty Newtonsoft.json General Is

DoNet2.0 need to use the Open source class library Newtonsoft.Json.dll

Code using system;using system.io;using system.text;using newtonsoft.json;namespace offlineacceptcontrol.uctools{    public class Jsontools    {        //Generate JSON string from an object information public        static string Objecttojson (Object obj)        {           return Javascriptconvert.serializeobject (obj);        }        Generate object information from a JSON string public        static Object Jsontoobject (string jsonstring, Object obj)        {           return Javascriptconvert.deserializeobject (jsonstring, obj. GetType ());}}}    

  

built-in method : Using the. NET Framework 3.5/ The JavaScriptSerializer class in the System.Web.Script.Serialization namespace provided in 4.0 is a straightforward object serialization and deserialization.

Project P = new Project () {Input = "stone", Output = "Gold"}; JavaScriptSerializer serializer = new JavaScriptSerializer (); var json = serializer. Serialize (P); Console.WriteLine (JSON); var P1 = serializer. Deserialize<project> (JSON); Console.WriteLine (P1. Input + "=" + p1. Output); Console.WriteLine (ReferenceEquals (P,P1));

Note: If you are using VS2010, you are required to change the target Framework of the current project to. Net Framework 4, and you cannot use client profile. Of course, this System.Web.Extensions.dll is mainly used by the Web, directly in the console project with a feeling a little waste of resources.
In addition, it can be seen from the last sentence that serialization and deserialization are typical implementations of deep copies .

contractual approach : using DataContractJsonSerializer or jsonreaderwriterfactory provided by System.Runtime.Serialization.dll to achieve

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

Code using system;using system.collections.generic;using system.io;using system.linq;using System.Text;using    System.runtime.serialization;using System.runtime.serialization.json;namespace CrjIIOfflineAccept.CrjIITools{           public class Jsontools {//From an object information generates a JSON string public static string Objecttojson (Object obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj.           GetType ());           MemoryStream stream = new MemoryStream (); Serializer.           WriteObject (stream, obj); byte[] databytes = new Byte[stream.           Length]; Stream.           Position = 0; Stream. Read (databytes, 0, (int) stream.           Length);        Return Encoding.UTF8.GetString (databytes); }//Generate object information from a JSON string public static object Jsontoobject (string jsonstring, Object obj) {Data Contractjsonserializer serializer = new DataContractJsonSerializer (obj.           GetType ()); MemoryStream mstream = new MemoryStream (Encoding.UTF8.           GetBytes (jsonstring)); Return serializer.        ReadObject (Mstream); }    }}

Note here that the project classes and Members here are related to the attribute:

[Datacontract]class project{    [DataMember] public    string Input {get; set;}    [DataMember]    public string Output {get; set;}}

Change the access permissions for classes and class variables to public entirely.

Practical Reference:
JSON validation Tool: http://jsonlint.com/
JSON Concise Tutorial: http://www.w3school.com.cn/json/

. How to parse JSON under net

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.