我正在尝试反序列化嵌套的json字符串
public class recentlySearchedAdd
{
public recentlySearchedAdd()
{
searchedLocations = new List<recentlySearchedLoc>();
}
public string status { get; set; }
public List<recentlySearchedLoc> searchedLocations { get; set; }
}
public class recentlySearchedLoc
{
public int id { get; set; }
public string location { get; set; }
}
这是处理json字符串
的代码 //dummy json string
string json = "{\"status\": \"OK\", \"searchedLocations\": [{\"id\": 7, \"location\": \"California\"}, {\"id\": 4, \"location\": \"90007\"}, {\"id\": 3, \"location\": \"New York, NY\"}]}";
JavaScriptSerializer ser = new JavaScriptSerializer();
List<recentlySearchedAdd> recentlySearchedAddList = ser.Deserialize<List<recentlySearchedAdd>>(json);
Response.Write("count:"+recentlySearchedAddList.Count);
计数为0 ......此代码有什么问题
答案 0 :(得分:3)
你很亲密。序列化程序返回的是recentlySearchedAdd
而不是它的列表。
JavaScriptSerializer ser = new JavaScriptSerializer();
recentlySearchedAdd recentlySearchedAddList = ser.Deserialize<recentlySearchedAdd>(json);
Console.Write("count:" + recentlySearchedAddList.searchedLocations.Count);