为了账号安全,请及时绑定邮箱和手机立即绑定

动态/解析 Json 对象

动态/解析 Json 对象

C#
三国纷争 2021-06-03 14:02:48
在以下代码段中,我不明白“//this failed”下的代码与“//this works”下的代码(在创建 Azure 函数时作为样板代码提供)之间的区别。 注意:我意识到不需要强制转换为对象,但 ReadAsAsync 返回对象....public static class FunctionTest{    [FunctionName("TestFunction")]    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, TraceWriter log)    {        log.Info("C# HTTP trigger function processed a request.");        // parse query parameter        string name = req.GetQueryNameValuePairs()          .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)          .Value;        //if nothing in the query parameter - lets' look into the body        if (name == null)        {            //this fails            dynamic abc = (object) @"{""name"":""test""}";            string test = abc?.name;            // this works            dynamic data = await req.Content.ReadAsAsync<object>();            name = data?.name;        }        return name == null          ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")          : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);    }}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信