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/