2012-12-01 106 views
0

我試圖反序列化嵌套的JSON字符串C#無法反序列化嵌套的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的代碼...這個代碼有什麼問題

回答

3

你就近了。什麼序列化器返回是recentlySearchedAdd不是它的一個列表。

JavaScriptSerializer ser = new JavaScriptSerializer(); 
recentlySearchedAdd recentlySearchedAddList = ser.Deserialize<recentlySearchedAdd>(json); 
Console.Write("count:" + recentlySearchedAddList.searchedLocations.Count); 
+0

我不好,謝謝! – wahaha