我對使用JSON數據感興趣,並試圖從AWS網站反序列化定價數據。我已經構建了我的類(我做了Paste Special>將JSON數據粘貼爲類),但是當我試圖反序列化時,它涉及到Config類時會變得遲鈍。我相信我需要創建一個字典或做一個列表,但不知道如何正確嵌套。我嘗試了不同的東西,但似乎沒有任何工作。指出我在正確的方向,所以我可以弄明白,非常感謝。反序列化C#中的JSON數據#
public class Rootobject
{
public float vers { get; set; }
public Config config { get; set; }
}
public class Config
{
public string rate { get; set; }
public string valueColumns { get; set; }
public string currencies { get; set; }
public Region regions { get; set; }
}
public class Region
{
public string region { get; set; }
public Instancetype instanceTypes { get; set; }
}
public class Instancetype
{
public string type { get; set; }
public Size sizes { get; set; }
}
public class Size
{
public string size { get; set; }
public string vCPU { get; set; }
public string ECU { get; set; }
public string memoryGiB { get; set; }
public string storageGB { get; set; }
public Valuecolumn valueColumns { get; set; }
}
public class Valuecolumn
{
public string name { get; set; }
public Prices prices { get; set; }
}
public class Prices
{
public string USD { get; set; }
}
private static T _download_serialized_json_data<T>(string url) where T : new() {
using (var w = new WebClient()) {
var json_data = string.Empty;
// attempt to download JSON data as a string
try {
json_data = w.DownloadString(url);
}
catch (Exception) {}
// if string with JSON data is not empty, deserialize it to class and return its instance
return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
}
}
當我們看不到原始的JSON時,很難說出你的類應該是什麼樣子。另外請注意,捕獲異常並忽略它們幾乎不可能是一個好主意。 – 2014-12-07 12:41:07