FSharp.Data.JsonProvider-从类型获取json [英] FSharp.Data.JsonProvider - Getting json from types

查看:60
本文介绍了FSharp.Data.JsonProvider-从类型获取json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FSharp.Data.JsonProvider提供了一种将json转换为F#类型的方法.是否可以反向进行操作,即声明FSharp.Data.JsonProvider创建的类型之一的实例,将字段值设置为我需要的值,然后获取等效的json?

The FSharp.Data.JsonProvider provides a means to go from json to an F# type. Is it possible to go in the reverse direction i.e. declare an instance of one of the types created by FSharp.Data.JsonProvider, set the field values to be what I need, and then get the equivalent json?

我已经尝试过类似的事情,

I've tried things like this,

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>

let fred = Simple( 
            Age = 5, // no argument or settable property 'Age'
            Name = "Fred")

推荐答案

F#Data的最新版本现在支持此功能.请参见 http://fsharp.github.io/FSharp.Data/中的最后一个示例library/JsonProvider.html .

The latest version of F# Data now supports this. See the last example in http://fsharp.github.io/FSharp.Data/library/JsonProvider.html.

您的示例将是:

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let fred = Simple.Root(age = 5, name = "Fred")

这篇关于FSharp.Data.JsonProvider-从类型获取json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆