我收到以下錯誤反序列化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() –