如何解析特殊字符的 JSON 响应


如何

解析特殊字符的 JSON 响应?

当我将邮递员工具与此 URL 一起使用时,我会收到有效且预期的 JSON。

但是,当我在 .Net 中使用此 URL 发出 HTTP 请求时,我会收到特殊字符的响应。

代码如下:

    let url =      String.Format(TagsUrl, pageNumber |> string)
    let client =   httpClient APIBaseAddress
    let response = client.GetAsync(url) |> Async.AwaitTask 
                                        |> Async.RunSynchronously
   if response.IsSuccessStatusCode
         then let json =   response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously
              let result = JsonConvert.DeserializeObject<Response>(json)
              let tags =   result.items |> List.ofSeq 
                                        |> List.map (fun i -> i.name)
              tags
         else []

我还尝试将响应作为字节数组读取,但结果相同:

let url =      String.Format(TagsUrl, pageNumber |> string)
let client =   httpClient APIBaseAddress
let response = client.GetByteArrayAsync(url) |> Async.AwaitTask 
                                             |> Async.RunSynchronously
let json = Encoding.UTF8.GetString(response);
let result = JsonConvert.DeserializeObject<Response>(json)

附录:

[<Literal>]
let TagsUrl = "2.2/tags?page={0}&order=desc&sort=popular&site=stackoverflow&filter=!-.G.68grSaJm"
[<Literal>]
let APIBaseAddress = "https://api.stackexchange.com/"
type Item =     { name : string }
type Response = { items: Item list }
可以使用

F# 数据库中的 Http 实用工具。以下为我提供了诀窍:

Http.RequestString
  ( "https://api.stackexchange.com/2.2/tags" + 
    "?page=1&order=desc&sort=popular&site=stackoverflow&filter=!-.G.68grSaJm")

相关内容

  • 没有找到相关文章

最新更新