Given:
let json = """
{
"KeyValuePairs": "[{\"Key\" : \"one\", \"Value\" : \"1\"},{\"Key\" : \"two\", \"Value\" : \"2\"}]"
}
"""
let dict = JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>(json)
let kvpairs = dict.["KeyValuePairs"]
let kvlist = JsonConvert.DeserializeObject<KeyValuePair<System.String, System.String> List>(kvpairs)
let kvmap = [for kv in kvlist -> (kv.Key, kv.Value)] |> Map.ofList
Can I use one expression with pipes instead of intermediate variables? If so, what would that look like?
FWIW I tried:
let kvpairs = JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>(json)
|> (fun d -> d.["KeyValuePairs"])
but that gave me the error:
error FS0010: Unexpected infix operator in binding. Expected incomplete structured construct at or before this point or other
token.
Update: What about the other way around: piping into JsonConvert?
let kvpairs =
json
|> JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>
|> (fun d -> d.["KeyValuePairs"])
This gives me:
error FS0717: Unexpected type arguments
In your code
let kvpairs = JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>(json)
|> (fun d -> d.["KeyValuePairs"])
The pipe is not indented far enough.
let kvpairs = JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>(json)
|> (fun d -> d.["KeyValuePairs"])
or
let kvpairs = JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>(json)
|> (fun d -> d.["KeyValuePairs"])
or
let kvpairs =
JsonConvert.DeserializeObject<Dictionary<System.String, System.String>>(json)
|> (fun d -> d.["KeyValuePairs"])
will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.