<i id='vipnn'><tr id='vipnn'><dt id='vipnn'><q id='vipnn'><span id='vipnn'><b id='vipnn'><form id='vipnn'><ins id='vipnn'></ins><ul id='vipnn'></ul><sub id='vipnn'></sub></form><legend id='vipnn'></legend><bdo id='vipnn'><pre id='vipnn'><center id='vipnn'></center></pre></bdo></b><th id='vipnn'></th></span></q></dt></tr></i><div id='vipnn'><tfoot id='vipnn'></tfoot><dl id='vipnn'><fieldset id='vipnn'></fieldset></dl></div>
  • <legend id='vipnn'><style id='vipnn'><dir id='vipnn'><q id='vipnn'></q></dir></style></legend>
  • <small id='vipnn'></small><noframes id='vipnn'>

        • <bdo id='vipnn'></bdo><ul id='vipnn'></ul>

        <tfoot id='vipnn'></tfoot>

        如何从 Azure 函数返回 JSON

        时间:2023-10-05

        <i id='y2aRD'><tr id='y2aRD'><dt id='y2aRD'><q id='y2aRD'><span id='y2aRD'><b id='y2aRD'><form id='y2aRD'><ins id='y2aRD'></ins><ul id='y2aRD'></ul><sub id='y2aRD'></sub></form><legend id='y2aRD'></legend><bdo id='y2aRD'><pre id='y2aRD'><center id='y2aRD'></center></pre></bdo></b><th id='y2aRD'></th></span></q></dt></tr></i><div id='y2aRD'><tfoot id='y2aRD'></tfoot><dl id='y2aRD'><fieldset id='y2aRD'></fieldset></dl></div>

            • <bdo id='y2aRD'></bdo><ul id='y2aRD'></ul>
            • <legend id='y2aRD'><style id='y2aRD'><dir id='y2aRD'><q id='y2aRD'></q></dir></style></legend>
                <tbody id='y2aRD'></tbody>
              <tfoot id='y2aRD'></tfoot>

                  <small id='y2aRD'></small><noframes id='y2aRD'>

                  本文介绍了如何从 Azure 函数返回 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Azure Functions.但是,我觉得我被一些非常简单的事情难住了.我试图弄清楚如何返回一些基本的 JSON.我不确定如何创建一些 JSON 并将其返回到我的请求中.

                  I am playing with Azure Functions. However, I feel like I'm stumped on something pretty simple. I'm trying to figure out how to return some basic JSON. I'm not sure how to create some JSON and get it back to my request.

                  曾几何时,我会创建一个对象,填充其属性并对其进行序列化.所以,我开始走这条路:

                  Once upon a time, I would create an object, populate its properties, and serialize it. So, I started down this path:

                  #r "Newtonsoft.Json"
                  
                  using System.Net;
                  
                  public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
                  {
                      log.Info($"Running Function");    
                      try {      
                        log.Info($"Function ran");
                  
                        var myJSON = GetJson();
                  
                        // I want myJSON to look like:
                        // {
                        //   firstName:'John',
                        //   lastName: 'Doe',
                        //   orders: [
                        //     { id:1, description:'...' },
                        //     ...
                        //   ]
                        // }
                        return ?;
                      } catch (Exception ex) {
                          // TODO: Return/log exception
                          return null;
                      }
                  }
                  
                  public static ? GetJson() 
                  {
                    var person = new Person();
                    person.FirstName = "John";
                    person.LastName = "Doe";
                  
                    person.Orders = new List<Order>();
                    person.Orders.Add(new Order() { Id=1, Description="..." });
                  
                    ?
                  }
                  
                  public class Person 
                  {
                    public string FirstName { get; set; }
                    public string LastName { get; set; }
                    public List<Order> Orders { get; set; }
                  }
                  
                  public class Order
                  {
                    public int Id { get; set; }
                    public string Description { get; set; }
                  }
                  

                  但是,我现在完全停留在序列化和返回过程上.我想我习惯于在 ASP.NET MVC 中返回 JSON,其中一切都是一个动作

                  However, I'm totally stuck on the serialization and return process now.I guess I'm used to returning JSON in ASP.NET MVC where everything is an Action

                  推荐答案

                  这是一个 Azure 函数返回格式正确的 JSON 对象而不是 XML 的完整示例:

                  Here's a full example of an Azure function returning a properly formatted JSON object instead of XML:

                  #r "Newtonsoft.Json"
                  using System.Net;
                  using Newtonsoft.Json;
                  using System.Text;
                  
                  public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
                  {
                      var myObj = new {name = "thomas", location = "Denver"};
                      var jsonToReturn = JsonConvert.SerializeObject(myObj);
                  
                      return new HttpResponseMessage(HttpStatusCode.OK) {
                          Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
                      };
                  }
                  

                  在浏览器中导航到端点,您将看到:

                  Navigate to the endpoint in a browser and you will see:

                  {
                    "name": "thomas",
                    "location": "Denver"
                  }
                  

                  这篇关于如何从 Azure 函数返回 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数? 下一篇:是否可以从存在 Azure 函数的同一文件夹中读取文件

                  相关文章

                1. <i id='6NVLT'><tr id='6NVLT'><dt id='6NVLT'><q id='6NVLT'><span id='6NVLT'><b id='6NVLT'><form id='6NVLT'><ins id='6NVLT'></ins><ul id='6NVLT'></ul><sub id='6NVLT'></sub></form><legend id='6NVLT'></legend><bdo id='6NVLT'><pre id='6NVLT'><center id='6NVLT'></center></pre></bdo></b><th id='6NVLT'></th></span></q></dt></tr></i><div id='6NVLT'><tfoot id='6NVLT'></tfoot><dl id='6NVLT'><fieldset id='6NVLT'></fieldset></dl></div>
                2. <small id='6NVLT'></small><noframes id='6NVLT'>

                    <bdo id='6NVLT'></bdo><ul id='6NVLT'></ul>
                  <legend id='6NVLT'><style id='6NVLT'><dir id='6NVLT'><q id='6NVLT'></q></dir></style></legend>
                  <tfoot id='6NVLT'></tfoot>