2017-10-21 76 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);