JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?)
反序列化:
一个json字符串如下:
{
"BasicResponse":{"Succeed":1},
"RTDataSets":[
{
"tag":"IPV_YX_102",
"kksCode":"FEOSCOJODSKJOHKJFDBSINJKJSS05"
"RTDataValues":[
{
"Value":0.0,
"Time":0
}
]
},
{
<pre name="code" class="html"> "tag":"IPV_YX_102",
"kksCode":"FEOSCOJODSKJOHKJFDBSINJKJSS05"
"RTDataValues":[
{
"Value":0.0,
"Time":0
}
]
},
..
.
]
}
现在要得到kksCode和对应的Value:
//将json字符串转换为对象
JObject jObject = (JObject)JsonConvert.DeserializeObject(getString);
<span style="font-family:System;"> //将RTDataSets的json数组转化为下一级json对象数组jar[]</span>
<span style="font-family:System;"> </span> JArray jar = JArray.Parse(jObject["RTDataSets"].ToString());
//对收到的字符串进行解析
//局部变量
string tampAdds;
double value;
// Dictionary<string, double> _dict = new Dictionary<string, double>();
// List<Dictionary<string, double>> _list = new List<Dictionary<string, double>>();
<span style="font-family:System;"> for (int j = i; j < (i + 1000); j++)
{
//将JArray转化回JObject
</span> JObject job = JObject.Parse(jar[i].ToString());
tampAdds = (string)job["kksCode"];
//再将RTDataValues的json数组转化为下一级json对象数组jarr[]
JArray jarr = JArray.Parse(job["RTDataValues"].ToString());
<pre name="code" class="csharp"><span style="font-family:System;"> //再将JArray转化回JObject</span>
JObject jobs = JObject.Parse(jarr[0].ToString());value = (double)(jobs["Value"]);
}
具体JSON.NET的应用请点击该链接