反序列化 JSON 到字典 <string, string >
Deserialise JSON to Dictionary<string, string >
我敢肯定,当我从极其松散的 PHP 迁移到 F# 时,我会提出一连串愚蠢的问题。
这一篇应该是直截了当的。
我有一个 json 文件
{
“some_ key” : “some_value”
}
这将有一个包含数百个项目的列表,采用简单的键、值(字符串、字符串)格式。
在 F# 中,我想将该字符串解码为 dictionary<string, string>
。
看起来很明显,但我看到的所有建议似乎都非常冗长,所以假设我遗漏了一些简单的东西。
我有 Thoth 和 FSharp.Data 可以随意使用,但可以免费使用其他任何东西。
使用透特的魔法方法是Decode.keyValuePairs。
open Thoth.Json.Net
let json = """{
“some_ key” : “some_value”
}"""
let decoder : Decoder<(string * string) list> = Decode.keyValuePairs Decode.string
Decode.fromString decoder json |> Result.map dict
// Ok System.Collections.Generic.IDictionary<string,string>
如果您使用的是 FSharp.Data 而 Json 提供程序不是一个选项,您可以使用 Fleece 而不是 Thoth,它与 FSharp.Data
#r "nuget: Fleece.FSharpData"
open System.Collections.Generic
open Fleece.FSharpData
let (x: Result<Dictionary<string,string>, _>) = parseJson """{"some_ key" : "some_value"}"""
(*
val x : Result<Dictionary<string,string>,Fleece.FSharpData.DecodeError> =
Ok dict [("some_ key", "some_value")]
*)
函数 parseJson
可以处理文本,但如果您的 json 已经用 FSharp.Data 解析过,您可以改用 ofJson
。
一个纯粹的 .NET Core 示例,如果您只是追求简单的东西:
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
开箱即用的 .NET 解决方案可能如下所示:
open System.Text.Json
open System.Collections.Generic
let json = """{
"key": "value",
"foo": "bar"
}"""
let dic: IReadOnlyDictionary<string, string> =
JsonSerializer.Deserialize(json);
// Usage example
dic.["foo"] = "bar" // true
let someKey = "dimdi"
match dic.TryGetValue someKey with
| false, _ -> printfn "Missing key %s" someKey
| _, v -> printfn "Found %s" v
我敢肯定,当我从极其松散的 PHP 迁移到 F# 时,我会提出一连串愚蠢的问题。
这一篇应该是直截了当的。 我有一个 json 文件
{
“some_ key” : “some_value”
}
这将有一个包含数百个项目的列表,采用简单的键、值(字符串、字符串)格式。
在 F# 中,我想将该字符串解码为 dictionary<string, string>
。
看起来很明显,但我看到的所有建议似乎都非常冗长,所以假设我遗漏了一些简单的东西。
我有 Thoth 和 FSharp.Data 可以随意使用,但可以免费使用其他任何东西。
使用透特的魔法方法是Decode.keyValuePairs。
open Thoth.Json.Net
let json = """{
“some_ key” : “some_value”
}"""
let decoder : Decoder<(string * string) list> = Decode.keyValuePairs Decode.string
Decode.fromString decoder json |> Result.map dict
// Ok System.Collections.Generic.IDictionary<string,string>
如果您使用的是 FSharp.Data 而 Json 提供程序不是一个选项,您可以使用 Fleece 而不是 Thoth,它与 FSharp.Data
#r "nuget: Fleece.FSharpData"
open System.Collections.Generic
open Fleece.FSharpData
let (x: Result<Dictionary<string,string>, _>) = parseJson """{"some_ key" : "some_value"}"""
(*
val x : Result<Dictionary<string,string>,Fleece.FSharpData.DecodeError> =
Ok dict [("some_ key", "some_value")]
*)
函数 parseJson
可以处理文本,但如果您的 json 已经用 FSharp.Data 解析过,您可以改用 ofJson
。
一个纯粹的 .NET Core 示例,如果您只是追求简单的东西:
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
开箱即用的 .NET 解决方案可能如下所示:
open System.Text.Json
open System.Collections.Generic
let json = """{
"key": "value",
"foo": "bar"
}"""
let dic: IReadOnlyDictionary<string, string> =
JsonSerializer.Deserialize(json);
// Usage example
dic.["foo"] = "bar" // true
let someKey = "dimdi"
match dic.TryGetValue someKey with
| false, _ -> printfn "Missing key %s" someKey
| _, v -> printfn "Found %s" v