-5

我目前正在开发一个不和谐的机器人,我想“翻译”这个:

[
   {
      "command" : "ping",
      "response" : "pong"
   },
   {
      "command" : "hi",
      "response" : "hello"
   }
]

.net 代码看起来像这样

    private void RegisterPingCommand()
    {
        commands.CreateCommand("ping")
            .Do(async (e) =>
            {
                await e.Channel.SendMessage("pong");
            });
    } }
    private void RegisterHiCommand()
    {
        commands.CreateCommand("Hi")
            .Do(async (e) =>
            {
                await e.Channel.SendMessage("Hello");
            });
    } }
4

1 回答 1

2

您可以使用 Json.Net 反序列化您的 json。然后循环你的结果。

var actions = JsonConvert.DeserializeObject<List<MyAction>>(json);
foreach(var x in actions)
{
    commands.CreateCommand(x.command)
    .Do(async (e) =>
    {
        await e.Channel.SendMessage(x.response);
    });
}

public class MyAction
{
    public string command { get; set; }
    public string response { get; set; }
}
于 2016-10-26T21:21:59.590 回答