将JSON反序列化为Dictionary< string,string> [英] Deserialise JSON to Dictionary<string, string >
问题描述
当我从极其松散的PHP迁移到F#时,我肯定会有来自我的愚蠢问题.
I’m sure there will be a barrage of stupid questions from me as I migrate away from the extremely loose PHP to F#.
希望这应该很简单.我有一个json文件
This one should hopefully be straightforward. I have a json file
{
"some_ key" : "some_value"
}
它将以简单的键,值(字符串,字符串)格式列出几百个项目.
That will have a list of a few hundred items in a simple key,value (string,string) format.
在F#中,我想将该字符串解码为 dictionary< string,string>
.似乎很明显,但是我所看到的所有建议似乎都太冗长了,所以假设我缺少一些简单的东西.
In F# I’d like to decode that string into a dictionary<string, string>
.
Seems obvious enough but all suggestions I’ve seen seem extremely verbose so assume I’m missing something simple.
我可以使用Thoth和FSharp.Data,尽管可以免费使用其他任何软件.
I have Thoth and FSharp.Data at my disposal, though open to anything else free.
推荐答案
纯净的.NET Core示例,如果您只是简单的事情:
A pure .NET Core example if you are only after something simple:
let json = """{
"some_ key" : "some_value"
}"""
let data =
System.Text.Json.JsonDocument.Parse(json)
.RootElement
.EnumerateObject()
|> Seq.map (fun o -> o.Name, o.Value.GetString())
|> dict
这篇关于将JSON反序列化为Dictionary< string,string>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!