1、引用命名空間 System.Web.Script.Serialization(需添加.net引用system.web.extension.dll);2、定義符合json的樹形結構對象。例如伺服器返回的JSON字串格式如下:{
"method":"GetImageFullPathList",
"imgPathList":{
"imgPathList":[
"http://10.9.34.207/ImageData/Image/NormalImage///2012/02/18/05/02/-5-201202180258330682-0004916.jpg",
"http://10.9.34.207/ImageData/Image/NormalImage///2012/02/18/01/02/-1-201202180258330682-0004916.jpg",
"http://10.9.34.207/ImageData/Image/NormalImage///2012/02/18/03/02/-3-201202180258330682-0004916.jpg",
"http://10.9.34.207/ImageData/Image/NormalImage///2012/02/18/0A/02/-A-201202180258330682-0004916.jpg"
]
}
}則構建對象如下:
public class ImageFullPathList
{
public string method;
public innerImgPathList imgPathList;
}
public class innerImgPathList
{
public string[] imgPathList;
}
3、通過下面語句將伺服器端返回的JSON字串匹配到自訂的結構體裡。jsonStr是伺服器端返回的json字串。
JavaScriptSerializer serializer = new JavaScriptSerializer();
ImageFullPathList json = serializer.Deserialize<ImageFullPathList>(jsonStr);