C#Bungie API ExpandoObject问题

时间:2019-02-26 09:20:04

标签: c# json api discord

当尝试从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”时,我在控制台中遇到了错误,无法弄清楚我要去哪里。< / p>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

来自

foreach (dynamic user in content.Response.results)

收件人

foreach (dynamic user in content.Response)