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

Blazor中如何从JSON字符串提取ItemSections的ItemId属性

解决方案:提取ItemSections中的ItemId并展示

1. 调整数据模型类

修改你的GridList并新增一个极简的ItemSection类,只保留业务需要的字段(Json序列化时会自动忽略JSON中不匹配的冗余字段):

public class GridList
{
    public int Id { get; set; }
    public string Description { get; set; }
    public int Position { get; set; }
    public bool Enabled { get; set; }
    // 替换原List<string>为List<ItemSection>,仅映射需要的ItemId
    public List<ItemSection> ItemSections { get; set; }
    // 可选:若需展示用户基础信息,定义对应类;无需则可注释/删除
    public List<User> Users { get; set; }
}

// 仅包含需要提取的ItemId属性
public class ItemSection
{
    public int ItemId { get; set; }
}

// 可选:用于展示用户信息的极简类
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

2. 简化反序列化代码

无需先将JSON解析为JsonDocument,直接反序列化即可(模型已匹配所需字段):

result = await url.WithOAuthBearerToken(Token).GetStringAsync();
items = JsonSerializer.Deserialize<GridList>(result, new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
});

3. 更新表格展示逻辑

在表格的Item Sections列,提取每个ItemSection的ItemId并拼接成可读字符串:

<table class="table table-striped">
    <thead>
        <tr>
            <th>Id</th>
            <th>Description</th>
            <th>Position</th>
            <th>Enabled</th>
            <th>Item Sections</th>
            <th>Users</th>
            <th>Data</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in items)
        {
            <tr>
                <td>@item.Id</td>
                <td>@item.Description</td>
                <td>@item.Position</td>
                <td>@item.Enabled</td>
                <!-- 提取ItemId并拼接为逗号分隔的字符串 -->
                <td>@string.Join(", ", item.ItemSections.Select(s => s.ItemId.ToString()))</td>
                <!-- 可选:若定义了User类,展示用户ID+名称 -->
                <td>@string.Join(", ", item.Users.Select(u => $"{u.Id} - {u.Name}"))</td>
                <td>
                    <button class="btn btn-secondary" @onclick="(e => GetId(item.Id))" style="background: linear-gradient(to bottom, #0b2363, #3a0647);">Apri Pagina</button>
                </td>
            </tr>
        }
    </tbody>
</table>

额外优化:无需嵌套类的直接提取方案

如果不想新增嵌套类,可通过JsonDocument直接遍历提取目标字段,手动构建GridList对象:

result = await url.WithOAuthBearerToken(Token).GetStringAsync();
using var jsonDocument = JsonDocument.Parse(result);
var root = jsonDocument.RootElement;

var gridItem = new GridList
{
    Id = root.GetProperty("id").GetInt32(),
    Description = root.GetProperty("description").GetString(),
    Position = root.GetProperty("position").GetInt32(),
    Enabled = root.GetProperty("enabled").GetBoolean(),
    // 直接提取ItemId并转为字符串列表
    ItemSections = root.GetProperty("itemSections")
                        .EnumerateArray()
                        .Select(s => s.GetProperty("itemId").GetInt32().ToString())
                        .ToList(),
    // 可选:提取用户信息
    Users = root.GetProperty("users")
                .EnumerateArray()
                .Select(u => $"{u.GetProperty("id").GetInt32()} - {u.GetProperty("name").GetString()}")
                .ToList()
};

// 适配表格的foreach遍历,将单个对象转为列表
items = new List<GridList> { gridItem };

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 10:02:35