F#数据:JSON解析器.使用JsonExtensions [英] F# Data: JSON Parser. Using JsonExtensions
问题描述
这是我关于SO的第一个问题...所以不要严格判断=)
通常我在聊天室中的所有技术问题(相信我,很多=))./p>
最近,我们正在谈论 RosettaCode .我想补充一些任务代码到F#
其中之一是 JSON .
可能的解决方案之一是使用"F#数据:JSON解析器".所以我的问题与此相关.
此代码运行良好:
open FSharp.Data
open FSharp.Data.JsonExtensions
type Person = {ID: int; Name:string}
let json = """[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }]"""
json |> printfn "%s"
match JsonValue.Parse(json) with
| JsonValue.Array(x) ->
x |> Array.map(fun x -> {ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)})
| _ -> failwith "fail json"
|> Array.iter(fun x -> printfn "%i %s" x.ID x.Name)
打印:
[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }]
1 "First"
2 "Second"
但是
{ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)}
看起来不太好.
这我了解了有关JsonExtensions的信息,
但是当我使用
{ID = (x?ID.AsInteger()) ; Name = (x?Name.AsString()) }
我收到编译错误:
-
未定义字段,构造函数或"AsInteger"
-
未定义字段,构造函数或"AsString"
奇怪的是,我通过打开FSharp.Data.JsonExtensions"看到了可访问性
所以,问题是:如何使用JsonExtensions?
我尝试使用一个最小的示例来重现此问题,但我没有收到错误-您可以尝试以下最小的示例吗?
#r "...../FSharp.Data.dll"
open FSharp.Data.JsonExtensions
open FSharp.Data
JsonValue.Parse("A").AsArray()
|> Array.map (fun a -> a?ID.AsInteger())
我没有在a?ID.
上获得自动补全功能(这是编辑器的局限性),但是可以正常编译.
我认为这可能不起作用的唯一原因是,如果您有另一个open
声明,该声明将导入?
运算符的另一个实现,且该实现不返回JsonValue
.
JsonValue
API肯定不如仅使用类型提供程序那么好-因此,如果可以的话,我可能会选择类型提供程序(如果您需要遍历所有内容,则低级API很好. JSON中).
It is my first question on SO...so do not judge strictly =)
Usually all my questions techout in chat rooms (believe me, a lot of them =)).
Recently, we are talking about the RosettaCode. And I wondered to complement some of the tasks code to F#
One of them is JSON.
One of the possible solutions is the use of "F# Data: JSON Parser". So my question is linked with it.
This code works well:
open FSharp.Data
open FSharp.Data.JsonExtensions
type Person = {ID: int; Name:string}
let json = """[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }]"""
json |> printfn "%s"
match JsonValue.Parse(json) with
| JsonValue.Array(x) ->
x |> Array.map(fun x -> {ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)})
| _ -> failwith "fail json"
|> Array.iter(fun x -> printfn "%i %s" x.ID x.Name)
Print:
[ { "ID": 1, "Name": "First" }, { "ID": 2, "Name": "Second" }]
1 "First"
2 "Second"
But it
{ID = System.Int32.Parse((x?ID).ToString()); Name = (string x?Name)}
doesn't look good.
This I read about JsonExtensions,
but when I use
{ID = (x?ID.AsInteger()) ; Name = (x?Name.AsString()) }
I get compile errors:
The field, constructor or "AsInteger" is not defined
The field, constructor or "AsString" is not defined
Strangely, thing is that I see accessibility through "open FSharp.Data.JsonExtensions"
So, question: How to use JsonExtensions?
I tried to reproduce this using a minimal example, but I do not get the error - can you try the following minimal sample?
#r "...../FSharp.Data.dll"
open FSharp.Data.JsonExtensions
open FSharp.Data
JsonValue.Parse("A").AsArray()
|> Array.map (fun a -> a?ID.AsInteger())
I do not get auto-completion on a?ID.
(which is a limitation of the editor), but it compiles fine.
The only reason why I think this could be not working is if you had another open
declaration that would import another implementation of the ?
operator that is not returning JsonValue
.
The JsonValue
API is certainly not as nice as just using the type provider - so if you can, I'd probably go for the type provider instead (the low-level API is good if you need to iterate over everything in JSON recursively).
这篇关于F#数据:JSON解析器.使用JsonExtensions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!