【问题标题】:C# Bungie API ExpandoObject issueC# Bungie API ExpandoObject 问题
【发布时间】:2019-07-19 19:02:40
【问题描述】:

在尝试从 Bungie API 读取 JSON 响应时,我似乎遇到了一些问题,以下方法通常有效,但由于某种原因,我现在收到错误消息。

功能代码

public async Task<List<string>> GetMemberID(string MembersName)
    {
        List<string> MembershipID = new List<string>();

        HttpResponseMessage response = await client.GetAsync(StaticObjects.bungieBasePath + $@"/User/SearchUsers/?q={MembersName}");

        if (response.IsSuccessStatusCode)
        {
            try
            {
                Console.WriteLine(await response.Content.ReadAsStringAsync());
                dynamic content = response.Content.ReadAsAsync<ExpandoObject>().Result;
                foreach (dynamic user in content.Response.results)
                {
                    MembershipID.Add(user.membershipId);
                }
            }
            catch
            {
                throw new ArgumentException("The member could not be found.");
            }
        }
        else
        {
            throw new ArgumentException("An error occurred retrieving the members information.");
        }

        return MembershipID;
    }

命令代码

[Command("invite")]
    [RequireContext(ContextType.Guild, ErrorMessage = "This command is specific to a particular server so you must send it from a channel within that server")]
    public async Task JoinDateAsync([Remainder]string MemberName)
    {
        using (Context.Channel.EnterTypingState())


            if (!Context.IsPrivate) await Context.Message.DeleteAsync();

        if (StaticObjects.CheckUserIsAdmin(Context))

        {
            List<string> MembershipID = await StaticObjects._bungie.GetMemberID(MemberName);

        }
    }

第一部分一切正常,我可以在控制台中看到 JSON 响应,但是当我尝试提取“membershipId”时,我在控制台中收到错误,无法弄清楚我哪里出错了。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# json api discord


    【解决方案1】:

    我发现了我的问题,JSON 的响应略有不同。在我删除一个词后,它现在可以工作了。

    来自

    foreach (dynamic user in content.Response.results)
    

    foreach (dynamic user in content.Response)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2021-07-05
      • 2011-10-03
      • 1970-01-01
      • 2012-02-28
      • 2019-01-03
      • 1970-01-01
      相关资源
      最近更新 更多