简体   繁体   English

F#JsonValue AsArray在哪里?

[英]F# Where is JsonValue AsArray?

I have: 我有:

open FSharp.Data
open FSharp.Data.JsonExtensions

[<EntryPoint>]
let testjson argv =

    let json = """
    {
        "KeyValuePairs": "[{\"Key\" : \"one\", \"Value\" : \"1\"},{\"Key\" : \"two\", \"Value\" : \"2\"}]"
    }
    """

    let info = JsonValue.Parse(json)
    let infoarray = info.AsArray()

But I'm getting an error on the last line: 但是我在最后一行出现错误:

Error 52 The field, constructor or member 'AsArray' is not defined 错误52字段,构造函数或成员'AsArray'未定义

even though I'm following the example here: 即使我在这里遵循以下示例:

F# Data: JSON Parser F#数据:JSON解析器

in the section, "Using JSON extensions". 在“使用JSON扩展”部分中。 So, clearly I've got this wrong.But where? 所以,显然我错了,但是在哪里呢?

You're trying to treat the JsonValue itself as an array - it's not, it's an object. 您正在尝试将JsonValue本身视为一个数组-不是,它是一个对象。 It seems you probably want to treat the KeyValuePairs property as an array. 看来您可能想将KeyValuePairs属性视为一个数组。 So try this: 所以试试这个:

let keyValuePairs = info?KeyValuePairs.AsArray()

This an old question but I will leave a reply for those in need. 这是一个古老的问题,但我将为有需要的人留下答复。

The mistake was when KeyValuePairs is seen as an Array but instead is a String 错误是当KeyValuePairs被看作是一个数组,而是一个字符串时

let json = """ {"KeyValuePairs": "[{\"Key\" : \"one\", \"Value\" : \"1\"},{\"Key\" : \"two\", \"Value\" : \"2\"}]"} """

let info = JsonValue.Parse(json)

let infoarray = info?KeyValuePairs.AsString()

let newinfo = JsonValue.Parse infoarray

printfn "%A" (newinfo.[0]?Key.AsString()) // prints "one"

Hope this was helpful! 希望这对您有所帮助!

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM