我从网络 api 得到响应,它只返回与请求 url 中的 id 对应的数据的 F# 记录。
我的问题是如何获取 response.Body 的文本表示,以便我可以将其插入下面注释掉的行并将其与 json 类型提供程序一起使用。或者有没有我想念的不同方法,
let response = Http.Request("http://localhost:50442/api/getrates/" + input)
let sample = Json.Sample.Parse(""" {"CurrencyId":1,"CurrencyCode":"AUD","Value":0.98542} """)
//let sample = Json.Sample.Parse("\"\"\" " + response.Body.ToString() + " \"\"\"")
let data = sample.Value
return "Rate for: " + input + " = " + data.ToString()
最佳答案
您需要一些示例 JSON 供类型提供程序在编译时检查(来自本地文件、文字字符串或来自远程 URL),用于检测架构和生成类型。
然后在运行时动态加载一些与相同模式匹配的新数据集,并且您具有类型化访问权限。
// provide some URL, file path, or raw JSON sample
// that can be used at compile time to generate types
type Json = JsonProvider<"sample.json">
// at runtime, load up new sample matching that schema
let response = Http.Request("<some url that serves JSON at runtime>")
let samples = Json.Parse(response.Body.ToString())
// or do it this way
let samples = Json.Load("<some url that serves JSON at runtime>")
// magic!
sample.[0].EyeColor
关于http - F# - Http 请求响应主体到字符串以与 json 类型提供程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29283350/