【发布时间】: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”时,我在控制台中收到错误,无法弄清楚我哪里出错了。
任何帮助将不胜感激。
【问题讨论】: