使用C#

时间:2019-05-25 13:42:27

标签: twilio

我已经翻阅了文档并阅读了相关文章,但是我仍然无法成功为包含多个元素的Action数组构造Json。

第一个元素是一个“记住”动作,其中包含自己的几个元素。第二个Action元素是一个“ Collect”操作。

我能够添加第一个元素,但是第二个元素使我难以理解。我发布的代码包含错误。我无法添加第二个“收集”元素。我要关闭还是要离开?任何指导都将不胜感激。

[HttpPost]
[Route("AcceptTask")]
internal string BuildAcceptCall(UserData ud)
{
    log.Debug("Entering BuildAcceptCall");
    var j = new
    {
        actions = new[]
        {
            new
           {
                remember = new
                   {
                         ud.AccountId,
                        EngineId = ud.EngineId,
                        ResidentTelephone = ud.ResidentTelephone,
                        OutboundCallerId = ud.OutboundCallerId,
                        UnitNumber = ud.UnitNumber,
                        BuildingNumber = ud.BuildingNumber,
                        Pets = ud.Pets,
                        Alarm = ud.Alarm,
                        EmergencyId = ud.EmergencyId,
                        CallDate = ud.CallDate,
                        WorkOrder = ud.WorkOrder,
                        CurrentLocation = ud.CurrentLocation
                   }, //close remember element

               collect = new
                {
                    name = "DidTechAcceptCall",
                    questions = new[]
                    {
                       new
                           {
                               question = "This is a maintenance call from Spring Meadows. will you accept the call?",
                               name = "OffferCallToTech",
                               type="Twilio.YES_NO"
                            }
                    },
                    on_complete = new
                    {
                        redirect = new
                        {
                            uri = @"https://8.40.212.131/MedsDispatch/api/Dispatch/EntryPoint_AutoPilot",
                            method = "post"
                        }
                    }
               }//close the collect element
            } //close the new
        }; //close the array

    string theObject = Newtonsoft.Json.JsonConvert.SerializeObject(j); ;
    return Newtonsoft.Json.JsonConvert.SerializeObject(j);

}

1 个答案:

答案 0 :(得分:0)

我强烈建议您执行以下一项或全部操作

无论如何,这是代码

var j = new { actions = new object[]{
                new{
                    remember = new{
                        data = "data"
                    }
                },
                new{
                    collect = new{
                        name = "DidTechAcceptCall",
                        questions = new[]
                        {
                           new
                               {
                                   question = "This is a maintenance call from Spring Meadows. will you accept the call?",
                                   name = "OffferCallToTech",
                                   type="Twilio.YES_NO"
                                }
                        },
                        on_complete = new
                        {
                            redirect = new
                            {
                                uri = @"https://8.40.212.131/MedsDispatch/api/Dispatch/EntryPoint_AutoPilot",
                                method = "post"
                            }
                        }
                    }
                }
     }
};
string theObject = Newtonsoft.Json.JsonConvert.SerializeObject(j);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(j));

产生

{
  "actions": [
    {
        "remember": {
            "data": "data"
        }
    },
    {
        "collect": {
            "name": "DidTechAcceptCall",
            "questions": [
                {
                    "question": "This is a maintenance call from Spring Meadows. will you accept the call?",
                    "name": "OffferCallToTech",
                    "type": "Twilio.YES_NO"
                }
            ],
            "on_complete": {
                "redirect": {
                    "uri": "https://8.40.212.131/MedsDispatch/api/Dispatch/EntryPoint_AutoPilot",
                    "method": "post"
                }
            }
        }
    }
  ]
}
相关问题