我收到以下错误反序列化JSON一些和试图访问root.items后访问属性:无法成功后反序列化JSON
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与模型匹配,则无法查看。 – Nikolaus
我的猜测是:http请求没有返回你期望的响应,或者ReadAsAsync不能如你所期望的那样工作。我会尝试用一个带有json的StringContent对象替换http请求,以帮助缩小问题的范围。 – Foole
嗨。测试上方定义的响应记录类型是“模型”。 response.Content.ReadAsAsync() –