C # Background parsing JSON

Source: Internet
Author: User

If the background is a JSON string format as follows:

String str = "{\" success\ ": True,\" msg\ ": \" Succeeded! \"}";

We turn to dynamic and get a property inside.

Dynamic response = Newtonsoft.Json.JsonConvert.DeserializeObject (str);

String a = Response. Success; String B = Response. MSG;

There are two more complex ways to do this:

Data data1 = jsonconvert.deserializeobject<data> (str); get directly: Data1. Success.

or:list<data> List = jsonconvert.deserializeobject<list<data>> (str); get required loop fetch: foreach (var item in List
{
Item. Success
}

However, all of the above 2 methods need to declare the class

public class Data
{
public string Success {get; set;}
public string MSG {get; set;}
}

If the background passes through it is a JSON (new {success = response. Success, msg = response. MSG}, jsonrequestbehavior.allowget); format; go directly to the dynamic format. Get: ResulOb.Data.success.

--------------Off-Topic

In fact, it can be passed directly to the front desk in this way:

Public Jsonresult Projectpush () {

Json (New {success = false, MSG = "Success! "}, Jsonrequestbehavior.allowget);

}

The front desk gets direct: var result = Ext.decode (Repsonse.responsetext); result.success. Of course, this is based on ext. You can go back and adjust the other frames.

--------------------------------------------------------------------------------------------------------Split Line---- --------------------------------------------------------------------------------------------------------------- -----------------------

Newtonsoft.json conversion between JSON and objects is a very powerful tool.

Object Conversion JSON string

Newtonsoft.Json.JsonConvert.SerializeObject ()

JSON string converted to specified object

Newtonsoft.json.jsonconvert.deserializeobject<> ()

JSON string gets the specified element value directly (like XML)

var jobject=jobject.parse (jsonstring); var time= jobject["Time"]. ToString ();//Get Time{"time": "2016-06-06 11:11:11"}

XML conversion JSON string

XmlDocument doc = new XmlDocument ();  Doc. LOADXML (postbackxml); var jobject = Newtonsoft.Json.JsonConvert.SerializeXmlNode (doc);//xml to Json

When the XML above is serialized, the root directory is usually present, and the object element that is to be serialized is the one that appears in the root directory.

Convert to Jobject first, using jobj["Mongotask"] as follows

var json=jsonconvert.serializexmlnode (item); var jobj = jsonconvert.deserializeobject<jobject> (JSON); Tasklist. ADD (jobj["Mongotask"]. Toobject<mongotask> ());

If you need to get the value of an attribute in XML, such as Get <user name= ' Zhang San ' > Get Zhang San when name Zodiac with the @ symbol

The code is filtered to the @ symbol to get a normal value.

var jobj = newtonsoft.json.jsonconvert.deserializeobject<jobject> (Jobject.replace ("@", "")), Var name= jobj[" Response "[" Body "] [" Orderresponse "] [" name "];

Summary:

1: Parse string directly
var jobject=jobject.parse (jsonstring); var time= jobject["Time"]. ToString ();//Get Time{"time": "2016-06-06 11:11:11"}

2: By turning it into dynamic and then getting a property inside it.

Dynamic response = Newtonsoft.Json.JsonConvert.DeserializeObject (str);

String a = Response. Success; String B = Response. MSG;

3: First construct an object, then transform the JSON string into an object, get its properties


C # Background parsing JSON

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.