为什么RestSharp无法反序列化接收到的JSON?

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

我正在尝试检索我的 JIRA 实例的用户列表:

options.Authenticator = new HttpBasicAuthenticator(_jiraConfig.UserName, _jiraConfig.Token);
options.BaseUrl = new Uri(_jiraConfig.Url);
_restClient = new RestClient(options);
var request = new RestRequest($"rest/api/2/group/member?groupname={_jiraConfig.GroupName}&includeInactiveUsers=false&startAt={start}&maxResults=100", Method.Get);
PagedQueryResult<JiraGroupUser> jiraUsers = await _restClient.GetAsync<PagedQueryResult<JiraGroupUser>>(request);

我正确收到结果:

{
    "self": "https://xxx.atlassian.net/rest/api/2/group/member?includeInactiveUsers=false&maxResults=50&groupId=f4a5587d-92ec-4cbf-824e-8489f38d147f&startAt=0",
    "nextPage": "https://xxx.atlassian.net/rest/api/2/group/member?includeInactiveUsers=false&maxResults=50&groupId=f4a5587d-92ec-4cbf-824e-8489f38d147f&startAt=50",
    "maxResults": 50,
    "startAt": 0,
    "total": 94,
    "isLast": false,
    "values": [
        {
            "self": "https://xxx.atlassian.net/rest/api/2/user?accountId=yyy",
            "accountId": "yyy",
            "avatarUrls": {
                "48x48": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/48",
                "24x24": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/24",
                "16x16": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/16",
                "32x32": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/32"
            },
            "displayName": "ABC",
            "active": true,
            "timeZone": "Europe/Zurich",
            "accountType": "atlassian"
        },
        {
            "self": "https://xxx.atlassian.net/rest/api/2/user?accountId=zzz",
            "accountId": "zzz",
            "avatarUrls": {
                "48x48": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/48",
                "24x24": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/24",
                "16x16": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/16",
                "32x32": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/32"
            },
            "displayName": "DEF",
            "active": true,
            "timeZone": "Europe/Zurich",
            "accountType": "app"
        },
        //....
    ]
}

我正在尝试反序列化这些对象:

internal class PagedQueryResult<T> : IEnumerable<T>
{
    public List<T> Values { get; set; } = new List<T>();
    public int StartAt { get; set; }
    public int MaxResults { get; set; }
    public int Total { get; set; }
    public bool IsLast { get; set; }
    public string Self { get; set; }
    public string NextPage { get; set; }


    public PagedQueryResult()
    {
    }



    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    public IEnumerator<T> GetEnumerator()
    {
        return Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return Values.GetEnumerator();
    }
}

public class JiraGroupUser
{
    public string AccountId { get; set; }
    public string AccountType { get; set; }
    public bool Active { get; set; }
    public string DisplayName { get; set; }
    public string EmailAddress { get; set; }
    public string TimeZone { get; set; }
    public string Self { get; set;}

}

奇怪的是,这段代码适用于一个相当旧的 RestSharp 版本,但更新到最后一个版本后,它不再工作了。

现在我收到此错误,这对我没有帮助:

System.Text.Json.JsonException: 'The JSON value could not be converted to CloudToolsUsersExporter.JiraExporter.Models.PagedQueryResult`1[CloudToolsUsersExporter.JiraExporter.Models.JiraGroupUser]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'

我唯一能看到的是 JSON 有一些我不需要的附加字段(如 avatarUrls),因此模型中没有。

我错过了什么?

c# json .net restsharp jsonserializer
1个回答
0
投票

RestSharp(可能)更新了他们用来反序列化 JSON 的库,新版本看到

PagedQueryResult
实现了
IEnumerable
,因此它期望一个 JSON 数组作为根对象,但实际返回值是一个 JSON 对象,所以为什么

路径:$ |行号: 0 |字节位置内联:1

解析器期望

[
作为第一个字符。

要解决此问题,请从

: IEnumerable<T>
类定义中删除
PagedQueryReult


附注通常建议不要在非集合的类型上实现/继承

IEnumerable
/集合类。
PagedQueryResult
不是一个集合,它是一个具有一些元数据和作为子属性的集合的对象。

© www.soinside.com 2019 - 2024. All rights reserved.