【问题标题】:Unable to access property after successfully deserializing JSON成功反序列化 JSON 后无法访问属性
【发布时间】:2018-04-02 18:54:17
【问题描述】:

我在反序列化一些 JSON 并尝试访问 root.items 后收到以下错误:

 root.items.Count |> should equal 2

System.NullReferenceException: '对象引用未设置为 对象的实例。'

root.items@ 为空。

JSON 如下:

{
    "items": [
        {
            "snippet": {
                "title": "Nikeza: F# Backwards Pipe Operator",
                "tags": [
                    "Elm",
                    "F#",
                    "Giraffe",
                    "Functional Programming",
                    "Software Development"
                ]
            }
        },
        {
            "snippet": {
                "title": "Giraffe: VS Code bug that doesn't show up in VS 20017 (3)",
                "tags": [
                    "Hangouts On Air",
                ]
            }
        },
        {
            "snippet": {
                "title": "Software Craftsmanship Conference - London",
                "tags": [
                    "Programming",
                    "Software Development",
                ]
            }
        }
    ]
}

这是我的代码:

[<CLIMutable>]
type Snippet =    { title: string; tags: Collections.Generic.List<String> }

[<CLIMutable>]
type Item =       { snippet : Snippet }

[<CLIMutable>]
type Response =   { items : Collections.Generic.List<Item> }

[<Test>]
let ``Apply tags to videos`` () =
    let  delimitedIds = ["id1";"id2";"id3"] |> String.concat ","
    let  url = String.Format(requestTagsUrl, apiKey, delimitedIds)
    let response = httpClient.GetAsync(url) |> Async.AwaitTask |> Async.RunSynchronously

    if response.IsSuccessStatusCode
        then let root = response.Content.ReadAsAsync<Response>() |> Async.AwaitTask |> Async.RunSynchronously
             root.items.Count |> should equal 2
        else Assert.Fail()

【问题讨论】:

  • 你能添加你的模型类的代码吗?否则无法查看 Json 是否与模型匹配。
  • 我的猜测是 http 请求没有返回您期望的响应,或者 ReadAsAsync 无法按您的预期工作。我会尝试用其中包含 json 的 StringContent 对象替换 http 请求,以帮助缩小问题范围。
  • 嗨。测试上面定义的响应记录类型是“模型”。 response.Content.ReadAsAsync()

标签: asp.net-web-api asp.net-core f# json.net


【解决方案1】:

我刚刚为它写了一个测试,我可以确认它的反序列化部分是正确的。检查请求是否正确返回响应。

open System
open Expecto

let json = 
    """
    {
        "items": [
            {
                "snippet": {
                    "title": "Nikeza: F# Backwards Pipe Operator",
                    "tags": [
                        "Elm",
                        "F#",
                        "Giraffe",
                        "Functional Programming",
                        "Software Development"
                    ]
                }
            },
            {
                "snippet": {
                    "title": "Giraffe: VS Code bug that doesn't show up in VS 20017 (3)",
                    "tags": [
                        "Hangouts On Air",
                    ]
                }
            },
            {
                "snippet": {
                    "title": "Software Craftsmanship Conference - London",
                    "tags": [
                        "Programming",
                        "Software Development",
                    ]
                }
            }
        ]
    }
    """

[<CLIMutable>]
type Snippet =    { title: string; tags: Collections.Generic.List<String> }

[<CLIMutable>]
type Item =       { snippet : Snippet }

[<CLIMutable>]
type Response =   { items : Collections.Generic.List<Item> }

[<Tests>]
let tests =
    testList "Test" [
        test "Testing deserialization" {
            let result : Response = Json.deserialize json
            Expect.equal result.items.Count 3 "Should have 3 items"
        }
    ]                

【讨论】:

  • 我找到了解决方案。再次感谢。
【解决方案2】:

following 为我工作:

总之,我不得不求助于使用以下方法检索实际的 json 字符串:

json = response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously

之后,我使用了 JsonConvert.DeserializeObject:

JsonConvert.DeserializeObject<Response>(json)

附录:

if response.IsSuccessStatusCode
    then let json = response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously
         let result = JsonConvert.DeserializeObject<Response>(json);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多