返回的单个或数组的应对数据



我正在使用本教程中的代码,并且在供应JSON上的典范中非常出色。

虽然这与单个对象非常好,但是当我从URL中恢复数组时,这无法处理。我尝试调整代码处理数组,但我认为上述代码目前比我的成绩高一步。

有人可以帮我找出我在做错什么吗?

这是我要工作的代码:

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"
        ]
    }
]

您的代码不远。唯一的问题是这一行:

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

应该是这样的:

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

如果您愿意使用List<T>而不是T[],则应该同样有效。然后,完整的方法将看起来像以下内容(请注意,我删除了尝试/捕获块,因为您没有做任何事情的例外 - 如果您只是要重新捕获,则无需抓住):

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);

相关内容

  • 没有找到相关文章

最新更新