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

如何在C#中正确解析包含图片数组的JSON数据

解决C#解析含图片数组的JSON并绑定到XAML的问题

1. 修改实体类适配图片数组

原PriceList类中Pict为单个字符串类型,需改为集合类型(推荐用List<string>,比数组更灵活)。同时注意JSON键与C#属性名的大小写匹配,通过JsonProperty特性确保反序列化时字段映射正确:

using Newtonsoft.Json;
using System.Collections.Generic;

public class PriceList
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("Rem")]
    public string Rem { get; set; }

    [JsonProperty("Price")]
    public string Price { get; set; }

    [JsonProperty("articul")]
    public string Articul { get; set; }

    [JsonProperty("Pict")]
    // 将单个字符串改为字符串集合
    public List<string> Pict { get; set; }
}

2. 反序列化代码无需改动

原反序列化逻辑保持不变,仅实体类内部属性类型调整,整体仍为IEnumerable<PriceList>集合:

this.BindingContext = JsonConvert.DeserializeObject<IEnumerable<PriceList>>(json);

3. XAML中绑定并展示图片数组

要在界面上显示多张图片,可使用ListView(列表展示)或CarouselView(轮播展示):

示例1:用ListView嵌套展示所有图片

<ListView ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding Name}" FontSize="Large"/>
                    <Label Text="{Binding Price}" FontSize="Medium"/>
                    <!-- 嵌套ListView展示当前商品的所有图片 -->
                    <ListView ItemsSource="{Binding Pict}" HeightRequest="200">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ImageCell ImageSource="{Binding .}" />
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

示例2:用CarouselView实现图片轮播

<CarouselView ItemsSource="{Binding}">
    <CarouselView.ItemTemplate>
        <DataTemplate>
            <StackLayout Padding="10">
                <Label Text="{Binding Name}" FontSize="Large"/>
                <Label Text="{Binding Price}" FontSize="Medium" Margin="0,0,0,10"/>
                <!-- 轮播展示当前商品的图片 -->
                <CarouselView ItemsSource="{Binding Pict}" HeightRequest="300">
                    <CarouselView.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding .}" Aspect="AspectFit"/>
                        </DataTemplate>
                    </CarouselView.ItemTemplate>
                </CarouselView>
            </StackLayout>
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>

进阶:兼容单图片与数组混合的JSON场景

若接口返回的JSON中Pict有时是单个字符串、有时是数组,可添加自定义JSON转换器实现兼容:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

public class SingleOrArrayConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(List<T>);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var token = JToken.Load(reader);
        return token.Type == JTokenType.Array 
            ? token.ToObject<List<T>>() 
            : new List<T> { token.ToObject<T>() };
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

然后在PriceList的Pict属性上添加转换器特性:

[JsonProperty("Pict")]
[JsonConverter(typeof(SingleOrArrayConverter<string>))]
public List<string> Pict { get; set; }

这样无论接口返回单图片字符串还是图片数组,都能正确反序列化为List<string>。

内容的提问来源于stack exchange,提问作者Валерий Севастьянов

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 18:49:14