日期:2014-05-18  浏览次数:21312 次

C# 后台解析json字符串
C# 后台 通过http get得到一个 json字符串
格式如下:
[
{"id":0,"email":"abc0@gmail.com","age":0},
{"id":1,"email":"abc1@gmail.com","age":2},
{"id":2,"email":"abc2@gmail.com","age":4},
{"id":3,"email":"abc3@gmail.com","age":6},
{"id":4,"email":"abc4@gmail.com","age":8}]


我想在想解析这个字符串 就是想得到 id email age 的值。


PS:wince程序 vs2005 创建的

------解决方案--------------------
这样

调用方法
List<Person> appResult = JSONToObject<List<Person>>(@"[{""id"":0,""email"":""abc0@gmail.com"",""age"":0},{""id"":1,""email"":""abc1@gmail.com"",""age"":2},{""id"":2,""email"":""abc2@gmail.com"",""age"":4},{""id"":3,""email"":""abc3@gmail.com"",""age"":6},{""id"":4,""email"":""abc4@gmail.com"",""age"":8}]");

for(int i = 0;i<appResult.Count;i++)
输出(appResult[i].id);

//定义
public class Person
{
public int id { set; get; }
public String email { set; get; }
public int age { set; get; }
}
public static T JSONToObject<T>(string jsonText)
{
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
try
{
return jss.Deserialize<T>(jsonText);
}
catch (Exception ex)
{
throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
}
}
------解决方案--------------------
get传递数据量也不大,自己写方法解析吧
------解决方案--------------------
http://blog.csdn.net/justsoloving/article/details/5794563
简单的jsonhelper
------解决方案--------------------
javascriptserial类估计也行吧。

------解决方案--------------------
Newtonsoft.Json.Net20