2017-10-21 77 views
0

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

你可以添加你的模型級的代碼嗎?否則,如果Json與模型匹配,則無法查看。 – Nikolaus

+0

我的猜測是:http請求沒有返回你期望的響應,或者ReadAsAsync不能如你所期望的那樣工作。我會嘗試用一個帶有json的StringContent對象替換http請求,以幫助縮小問題的範圍。 – Foole

+0

嗨。測試上方定義的響應記錄類型是「模型」。 response.Content.ReadAsAsync () –

回答

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" 
     } 
    ]     
+0

我找到了一個解決方案。再次感謝。 –

0

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);