将XML转换为JSON并转换为蛇形

时间:2019-11-20 08:51:46

标签: json xml function

我正在尝试将发送给邮递员的XML正文转换为蛇形情况下的Json。 有人可以帮我将JSON响应消息转换为蛇形案例。 到目前为止,我有以下代码:

public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {

       DefaultContractResolver contractResolver = new DefaultContractResolver
        {
          NamingStrategy = new SnakeCaseNamingStrategy()
        };

        if (req.ContentType.IndexOf(@"/xml", 0, System.StringComparison.OrdinalIgnoreCase) == -1)
        {
            return new BadRequestObjectResult(@"Content-Type header must be an XML content type");
        }

        XmlDocument doc = new XmlDocument();
        doc.Load(req.Body);   
        doc.RemoveChild(doc.FirstChild);

        string json = JsonConvert.SerializeObject(doc, new JsonSerializerSettings
        {
            ContractResolver = contractResolver,
            Formatting = Newtonsoft.Json.Formatting.Indented
        });

        return new OkObjectResult(json);

0 个答案:

没有答案