在Azure函数中将F#记录类型返回为JSON

编程入门 行业动态 更新时间:2024-10-23 17:33:38
本文介绍了在Azure函数中将F#记录类型返回为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在F#中创建一个简单的Azure函数.最后,我将返回一个记录类型为JSON.我正在做这样的事情:

I'm creating a simple azure function in F#. At the end, I'm returning a record type as JSON. I'm doing something like this:

let y = {Gender = "Woman"; Frequency = 17; Percentage = 100.0} req.CreateResponse(HttpStatusCode.OK, y);

当我从Postman调用函数时,我得到了这个JSON {"Gender@":"Woman","Frequency@":17,"Percentage@":100}

When I call the function from Postman I'm getting this JSON {"Gender@":"Woman","Frequency@":17,"Percentage@":100}

这似乎是由默认序列化程序引起的(将F#记录类型序列化为JSON,每个属性后都包含'@'字符).

It looks like that this is caused by the default serializer (Serializing F# Record type to JSON includes '@' character after each property).

然后我尝试使用Newtonsoft.Json.现在,代码如下所示:

Then I tried to use Newtonsoft.Json. Now, the code looks like this:

req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(y));

但是现在我使用邮递员来获取它:

But now I'm getting this using postman:

"{\"Gender\":\"Woman\",\"Frequency\":17,\"Percentage\":100}"

我想得到这个回复: {"Gender":"Woman","Frequency":17,"Percentage":100}

I'd like to get this response: {"Gender":"Woman","Frequency":17,"Percentage":100}

如何获取此JSON响应?除了指定DataMemberAttribute之外,还有其他方法吗?

How can I get this JSON response? Is there any other way apart from specifying DataMemberAttribute?

谢谢

推荐答案

我认为您不能使用JSON.Net(这很好),因为数据契约序列化程序似乎对AzureFunctions基础结构

I don't think you can use JSON.Net (would be nice) because the AzureFunctions infrastructure seems to the Data Contract Serializers

我已经按照将F#记录类型序列化为JSON并将每个属性后的'@'字符序列化为如果比您希望的更笨重,它将对我有用.

I have just implemented the fix as per Serializing F# Record type to JSON includes '@' character after each property and it works for me if a bit clunkier than you may hope.

我也在努力解决这个问题,您使我朝着正确的方向前进-谢谢

I was also struggling to fix this and you got me going in the right direction - Thanks

#r "System.Runtime.Serialization" open System.Runtime.Serialization [<DataContract>] type SearchItem = { [<field: DataMember(Name="Gender")>] Gender: string [<field: DataMember(Name="Frequency")>] Frequency: int [<field: DataMember(Name="Percentage")>] Percentage: float }

更多推荐

在Azure函数中将F#记录类型返回为JSON

本文发布于:2023-10-16 11:56:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   函数   类型   Azure   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!