反序列化数据返回单个或数组

时间:2014-02-03 22:18:04

标签: json.net

我正在使用this tutorial中的代码,它在将json反序列化为类时效果非常好。

虽然这对单个对象非常有效,但当我从url中获取一个数组时,这并不能解决这个问题。我尝试调整代码来处理数组,但我认为上面的代码目前比我的成绩高出一步。

有人可以帮我弄清楚我做错了吗?

以下是code我正在努力工作:

public static T[] GetEdmundsContents<T>(string url) where T : new()
{
    using (var client = new WebClient())
    {
        var jsonData = string.Empty;
        try
        {
            Sleep(1000);
            jsonData = client.DownloadString(url);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return !string.IsNullOrEmpty(jsonData) ? JsonConvert.DeserializeObject<T[]>(jsonData) : new T();
    }
}

static void Sleep(int ms)
{
    new System.Threading.ManualResetEvent(false).WaitOne(ms);
}

以下是我从Web服务(缩写)中获取的JSON:

[
    {
        "authorNames": [
            "Audi Press AG"
        ],
        "captionTranscript": "2013 Audi A6 3.0T Premium quattro Sedan Cargo Area",
        "type": "PHOTOS",
        "subType": "interior",
        "shotTypeAbbreviation": "CARGO",
        "photoSrcs": [
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_150.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_87.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_131.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_423.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_396.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_175.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_500.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_600.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_1600.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_400.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_2048.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_98.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_276.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_300.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_815.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_717.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_196.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_cargo_oem_1_185.jpg"
        ]
    },
    {
        "authorNames": [
            "Audi Press AG"
        ],
        "captionTranscript": "2013 Audi A6 3.0T Premium quattro Sedan Interior",
        "type": "PHOTOS",
        "subType": "interior",
        "shotTypeAbbreviation": "I",
        "photoSrcs": [
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_396.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_196.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_276.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_185.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_175.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_300.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_500.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_423.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_131.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_150.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_600.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_2048.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_87.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_717.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_1600.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_400.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_98.jpg",
            "/audi/a6/2013/oem/2013_audi_a6_sedan_30t-premium-quattro_i_oem_2_815.jpg"
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

你的代码并不遥远。唯一的问题是这一行:

return !string.IsNullOrEmpty(jsonData) 
    ? JsonConvert.DeserializeObject<T[]>(jsonData) 
    : new T();

应该是这样的:

return !string.IsNullOrEmpty(jsonData) 
    ? JsonConvert.DeserializeObject<T[]>(jsonData) 
    : new T[]();

如果您愿意,可以使用List<T>代替T[],它也可以正常使用。然后,完整的方法将如下所示(注意我删除了try / catch块,因为您没有对捕获的异常执行任何操作 - 如果您只是要重新抛出则无需捕获):

public static List<T> GetEdmundsContents<T>(string url) where T : new()
{
    using (var client = new WebClient())
    {
        Sleep(1000);
        string jsonData = client.DownloadString(url);
        return !string.IsNullOrEmpty(jsonData) 
            ? JsonConvert.DeserializeObject<List<T>>(jsonData) 
            : new List<T>();
    }
}

对于您发布的JSON,T应该是这样的类:

class CarSpec
{
    public List<string> authorNames { get; set; }
    public string captionTranscript { get; set; }
    public string type { get; set; }
    public string subType { get; set; }
    public string shotTypeAbbreviation { get; set; }
    public List<string> photoSrcs { get; set; }
}

然后你可以像这样调用你的方法:

List<CarSpec> specs = GetEdmundsContents<List<CarSpec>>(url);