You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整模型类以正确反序列化含动态属性的图片API响应?

Fixing JSON Deserialization for Dynamic Image Properties

Yep, you can absolutely fix this by using the [JsonExtensionData] attribute from Json.NET—it’s tailor-made for capturing those dynamic, numerically-keyed properties that don’t map to explicit model fields. Here’s why your current setup isn’t working, and how to adjust it:

Why Your Dictionary Is Null

Your ListImages property stays null because Json.NET only maps JSON properties to model properties when their names match exactly. In your API response, there’s no "ListImages" key—instead, you have "0", "1", etc. By default, the serializer ignores these unrecognized properties instead of stuffing them into your dictionary.

Modified Model Classes

Add the [JsonExtensionData] attribute to your ListImages dictionary to tell Json.NET to collect all unrecognized JSON properties into it. Here’s the updated code:

public class MyViewModel { 
    [JsonProperty("images")] 
    public ImagesViewModel Images { get; set; } 
}

public class ImagesViewModel { 
    [JsonProperty("totalCount")] 
    public int TotalCount { get; set; } 
    
    // This attribute captures all unrecognized JSON properties
    [JsonExtensionData]
    public Dictionary<string, JToken> ListImages { get; set; } 
}

public class ImageViewModel { 
    [JsonProperty("url")] 
    public string Url { get; set; } 
}

How to Access the Image Data

After deserialization, you can convert the JToken values in the dictionary to your ImageViewModel type to get the URLs:

// Your existing deserialization code
System.Net.Http.HttpResponseMessage response = await client.GetAsync(endpointUrl);
var jsonString = await response.Content.ReadAsStringAsync(); // Use await instead of .Result!
MyViewModel model = JsonConvert.DeserializeObject<MyViewModel>(jsonString);

// Iterate through the dynamic image entries
foreach (var imageEntry in model.Images.ListImages)
{
    var image = imageEntry.Value.ToObject<ImageViewModel>();
    string imageUrl = image.Url;
    // Do something with imageUrl (e.g., add to a list, display, etc.)
}

Note: I swapped jsonString.Result for await in your deserialization code—using .Result can cause deadlocks in async contexts, so await is the safer practice.

Bonus: Convert Directly to a List of Images

If you’d prefer a List<ImageViewModel> instead of a dictionary, you can add a helper property to ImagesViewModel to convert the extension data:

public class ImagesViewModel { 
    [JsonProperty("totalCount")] 
    public int TotalCount { get; set; } 
    
    [JsonExtensionData]
    private Dictionary<string, JToken> _imageData { get; set; }

    // Helper property to get images as a list
    public List<ImageViewModel> ListImages => _imageData?
        .Values
        .Select(token => token.ToObject<ImageViewModel>())
        .ToList() ?? new List<ImageViewModel>();
}

This way, you can directly access model.Images.ListImages as a ready-to-use list of image view models.

内容的提问来源于stack exchange,提问作者Steven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:45:36