【发布时间】:2016-06-29 23:13:10
【问题描述】:
我找到了这个F# JSON parsing - How to get property using complex path (consisting of several propery names)(我缩短了文件字符串)。我想知道如何在 FSI 窗口中显示 json 文件中的实际数据?
let soilTest = JsonValue.Load("c:\\,,,soiltest.json")
let query soilTest=
let node = JObject.Parse soilTest
node.SelectToken "person[0].name" |> string
这里是json文件的开头:
{
"person": [
{
"name": "John Doe",
"date": "December 1, 2015",
.....
我查看了 Mark Seemann 提供的链接,了解如何从 json 文件中获取数据并显示在控制台中。
然而,当把
Console.WriteLine(name)
我收到此警告:
warning FS0020: This expression should have type 'unit', but has type 'string'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name.
val query : soilTest:string -> unit
试过|>在console.writeline末尾忽略,我得到val query : soulTest:string -> unit
如何从这一点做一个 let 语句来绑定数据?
【问题讨论】:
-
首先,
query可能不是保留字,但我仍然会避免绑定到它。只需使用qry。name绑定到什么?在函数的最后一行你可能想说let name = node.SelectToken "person[0].name" |> string,然后在下一行说Console.WriteLine(name) |> ignore。但通常你应该能够用 Newtonsoft.JSON...和/或 json 提供程序做几乎任何事情。 -
当我更改或删除查询时,我收到此错误 FS0001: This expression was expected to have type string but here has type > JsonValue .
-
如何更改/删除查询?您能否显示整个代码,包括您拥有的任何打开的语句、您执行的内容和错误。你可以通过chat.stackoverflow.com/rooms/51909/f来避免在cmets中来回走动
-
顺便说一句,你可以避开整个
JObject.Parse,直接访问名字:soilTest.["person"].[0].["name"] |> string。 -
代码越简单越好,再次感谢
标签: json f# f#-interactive f#-data