. net2.0 C # JSON deserialization

Source: Internet
Author: User

Go: http://json.codeplex.com/download Json.NET 1.3.1 (. net2.0)

Json.NET api:http://james.newtonking.com/projects/json/help/

Unzip the bin file into the bin file of the project (I took the Newtonsoft.Json.dll configuration of the XML file without a copy yet know how to use it)

Deserialization: (That is, the JSON is taken out to give C # and then manipulate the data I understand)

Code:

Using newtonsoft.json;//Import Class

String Ojson = "[{SZD: ' BM ', SGX: ' = ', sz: ' + '},{SZD: ' SJ ', SGX: ' = ', SZ: ' 3333 '},{SZD: ' SJ ', SGX: ' = ', sz: ' 3333 '}]";

/* Get json*/

Newtonsoft.Json.JavaScriptArray JSA = (javascriptarray) javascriptconvert.deserializeobject (Ojson);

for (int i = 0; i < JSA. Count; i++)

{

Javascriptobject JSO = (javascriptobject) jsa[i];

/*javascriptobject[""] get the specific value */

Response.Write (jso["SZD"). ToString () +jso["SGX"]. ToString () +convert.toint32 (jso["SZ") + "</br>");

}

Goal: Parse a string of JSON strings similar to [{},{},{}] that host the array of data in C #. Deserializes it into a list of objects

Operating Environment:

NUnit2.4 C #. net2.0

Code:

Using System;

Using System.Collections.Generic;

Using System.Text;

Using Nunit.framework;

Using Newtonsoft.json;

Namespace Testprj

{

[Testfixture]

public class Testjson

{

[Test]

public void TestA ()

{

Test data

String Ojson = "[{SZD: ' BM ', SGX: ' = ', sz: ' + '},{SZD: ' SJ ', SGX: ' = ', SZ: ' 3333 '},{SZD: ' SJ ', SGX: ' = ', sz: ' 3333 '}]";

Newtonsoft.Json.JavaScriptArray JSA = (javascriptarray) javascriptconvert.deserializeobject (Ojson);

ilist<searchargs> li = new list<searchargs> ();

for (int i = 0; i < JSA. Count; i++)

{

Javascriptobject JSO = (javascriptobject) jsa[i];

Searchargs sa = new Searchargs (Getjsonstringvalue (JSO, "SZD"), Getjsonstringvalue (JSO, "SGX"), Getjsonstringvalue (JSO, "SZ"));

Li. ADD (SA);

}

System.Console.WriteLine (li. Count);

}

public class Searchargs {

public string SZD {get; set;}

public string SGX {get; set;}

public string SZ {get; set;}

Public Searchargs (String szd,string sgx,string sz) {

SZD = SZD;

SGX = SGX;

SZ = SZ;

}

}

public static string Getjsonstringvalue (Newtonsoft.Json.JavaScriptObject jso, string key)

{

Try

{

return Jso[key] = = null? String. Empty:jso[key]. ToString ();

}

catch (Exception e)

{

return string. Empty;

}

}

}

}

Transferred from: http://cjl20082002.blog.163.com/blog/static/120827332009511103457637/

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.