为什么Json反序列化器不工作?

问题描述 投票:0回答:1

我对 Json 文件和 Blazor 相当陌生,我试图反序列化 Json 字符串,但是当我尝试使用通过反序列化器获取的对象填充列表时,列表中充满了空对象。 我也在使用 Flurl,我不知道它是否有任何相关性,但我只是提一下以确保确定。

我要反序列化的字符串是这个:

[{"id":1,"description":"DASHBOARD","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":6,"description":"Accounting","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":7,"description":"Anti-avoidance obligations","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":8,"description":"Personal data","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":9,"description":"Date","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":10,"description":"Balance","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":11,"description":"Communication","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":12,"description":"F24 - F23 and communications","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":13,"description":"Management","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":14,"description":"StudyOK","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":15,"description":"730","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":16,"description":"770","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":17,"description":"Fees","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":18,"description":"Certification","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":19,"description":"IMU","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":20,"description":"ISA","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":21,"description":"IVA","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":22,"description":"PF","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":23,"description":"SC","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":24,"description":"SP","position":0,"enabled":true,"itemSections":[],"users":[]},{"id":25,"description":"Widget","position":0,"enabled":true,"itemSections":[],"users":[]}]

这是代码:

var tokenTrimmed = token2.Split("\"token\":\"")[1].Split('"')[0].Trim();
result = await url.WithOAuthBearerToken(tokenTrimmed).GetStringAsync();

var jsonDocument = JsonDocument.Parse(result);


Console.WriteLine(jsonDocument.RootElement.ToString());

items = JsonSerializer.Deserialize<List<GridList>>(jsonDocument.RootElement);

这是 GridList 类:

@code {
    public int Id { get; set; }
    public string Description { get; set; }
    public int Position { get; set; }
    public bool Enabled { get; set; }
    public List<string> ItemSections { get; set; }
    public List<string> Users { get; set; }
}

这就是我得到的:它们都是一样的

json blazor deserialization flurl
1个回答
0
投票

总结一下:

var options = new JsonSerializerOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

var result = JsonSerializer.Deserialize<List<GridList>>(yourJsonAsString, options);
© www.soinside.com 2019 - 2024. All rights reserved.