C# 无法在.Net Core中反序列化json?
如何在F#中执行json反序列化 我有以下代码:C# 无法在.Net Core中反序列化json?,c#,json,asp.net-web-api,f#,.net-core,C#,Json,Asp.net Web Api,F#,.net Core,如何在F#中执行json反序列化 我有以下代码: type Snippet = { title: String; tags: String list } [<Test>] let ``Apply tags to videos`` () = let response = httpClient.GetAsync(url) |> Async.AwaitTask |> Async.RunSynchronously if response.IsSuccessSt
type Snippet = { title: String; tags: String list }
[<Test>]
let ``Apply tags to videos`` () =
let response = httpClient.GetAsync(url) |> Async.AwaitTask |> Async.RunSynchronously
if response.IsSuccessStatusCode
then let tags = response.Content.ReadAsAsync<System.Collections.Generic.List<Snippet>>() |> Async.AwaitTask |> Async.RunSynchronously
总之,我正在使用C#的列表进行反序列化,我认为这是必须的
但是,我仍然不清楚我做错了什么?我认为这是因为您提供的JSON不是一个片段数组。它是一个具有属性(称为Items)的对象,该属性是一个片段数组
您可以创建一个具有该属性的类型并反序列化它(我听说这是最佳做法),或者将json更改为“裸”数组(没有容器项属性)。我认为这是因为您提供的json不是一个片段数组。它是一个具有属性(称为Items)的对象,该属性是一个片段数组
您可以创建一个具有该属性的类型并对其进行反序列化(我听说这是最佳做法),或者将json更改为“裸”数组(不带container items属性)。您的示例如下所示
type Response =
{ items : SnippetItem[] }
and SnippetItem =
{ snippet : Snippet[] }
and Snippet =
{ title: string
tags: string list }
[<Test>]
let ``Apply tags to videos`` () =
async {
let! response = httpClient.GetAsync(url) |> Async.AwaitTask
if response.IsSuccessStatusCode
then
let! tags = response.Content.ReadAsAsync<Response>() |> Async.AwaitTask
....
} |> Async.RunSynchronously
类型响应=
{items:SnippetItem[]}
和项目=
{代码段:代码段[]}
和代码段=
{标题:字符串
标记:字符串列表}
[]
让“`将标记应用于视频”`()=
异步的{
let!response=httpClient.GetAsync(url)|>Async.aittask
if response.issucessStatusCode
然后
让!tags=response.Content.ReadAsAsync()|>Async.task
....
}|>Async.RunSynchronously
您的示例类似于此
type Response =
{ items : SnippetItem[] }
and SnippetItem =
{ snippet : Snippet[] }
and Snippet =
{ title: string
tags: string list }
[<Test>]
let ``Apply tags to videos`` () =
async {
let! response = httpClient.GetAsync(url) |> Async.AwaitTask
if response.IsSuccessStatusCode
then
let! tags = response.Content.ReadAsAsync<Response>() |> Async.AwaitTask
....
} |> Async.RunSynchronously
类型响应=
{items:SnippetItem[]}
和项目=
{代码段:代码段[]}
和代码段=
{标题:字符串
标记:字符串列表}
[]
让“`将标记应用于视频”`()=
异步的{
let!response=httpClient.GetAsync(url)|>Async.aittask
if response.issucessStatusCode
然后
让!tags=response.Content.ReadAsAsync()|>Async.task
....
}|>Async.RunSynchronously
我引用此项以确定反序列化所需的以下类型:
[<CLIMutable>]
type Snippet = { title: string; tags: string [] }
[<CLIMutable>]
type Item = { snippet : Snippet }
[<CLIMutable>]
type RootObject = { items : Item[] }
...
if response.IsSuccessStatusCode
then let root = response.Content.ReadAsAsync<RootObject>() |> Async.AwaitTask |> Async.RunSynchronously
[]
类型代码段={title:string;tags:string[]}
[]
类型项={snippet:snippet}
[]
类型RootObject={items:Item[]}
...
if response.issucessStatusCode
然后让root=response.Content.ReadAsAsync()|>Async.AwaitTask |>Async.RunSynchronously
该问题似乎已得到解决。我引用此内容以确定反序列化所需的以下类型:
[<CLIMutable>]
type Snippet = { title: string; tags: string [] }
[<CLIMutable>]
type Item = { snippet : Snippet }
[<CLIMutable>]
type RootObject = { items : Item[] }
...
if response.IsSuccessStatusCode
then let root = response.Content.ReadAsAsync<RootObject>() |> Async.AwaitTask |> Async.RunSynchronously
[]
类型代码段={title:string;tags:string[]}
[]
类型项={snippet:snippet}
[]
类型RootObject={items:Item[]}
...
if response.issucessStatusCode
然后让root=response.Content.ReadAsAsync()|>Async.AwaitTask |>Async.RunSynchronously
这个问题似乎已经解决了
如何在F#中执行json反序列化
为了实现完整的功能,您可以将一元代码用于JSON
假设你的例子是
let exampleJson = response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously
let parsed = json().Parse exampleJson
您已经定义了目标类型
type Snippet = { title: String; tags: String list }
和一些助手一起
let jparseObject (JSObject items) = items
let jparseObjName jname = jparseObject >> Map.find jname
let jparseArray mapper (JSArray items ) = items |> Seq.map mapper
let jparseArrName jname mapper = Map.find jname >> jparseArray mapper
let jparseString (JSString str) = str
let jparseStrName jname = Map.find jname >> jparseString
那么你最终会做什么
let json2snippets = jparseObjName @"""items""" >>
jparseArray (jparseObjName @"""snippet""" >>
jparseObject >> fun snippet ->
{title =snippet |> jparseStrName @"""title""";
tags=snippet |> jparseArrName @"""tags""" jparseString |> Seq.toList })
其中(json2snippets parsed |>Seq.toList)
是一个片段列表
如何在F#中执行json反序列化
为了实现完整的功能,您可以将一元代码用于JSON
假设你的例子是
let exampleJson = response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously
let parsed = json().Parse exampleJson
您已经定义了目标类型
type Snippet = { title: String; tags: String list }
和一些助手一起
let jparseObject (JSObject items) = items
let jparseObjName jname = jparseObject >> Map.find jname
let jparseArray mapper (JSArray items ) = items |> Seq.map mapper
let jparseArrName jname mapper = Map.find jname >> jparseArray mapper
let jparseString (JSString str) = str
let jparseStrName jname = Map.find jname >> jparseString
那么你最终会做什么
let json2snippets = jparseObjName @"""items""" >>
jparseArray (jparseObjName @"""snippet""" >>
jparseObject >> fun snippet ->
{title =snippet |> jparseStrName @"""title""";
tags=snippet |> jparseArrName @"""tags""" jparseString |> Seq.toList })
其中
(json2snippets parsed |>Seq.toList)
是一个片段列表
不久前,我遇到了一个类似的问题,并发布了一个关于它的问题(除了我的问题是在C语言中…我假设解决方案在F语言中也能工作)。问题在于,您的数据实际上是一个带有字符串键和代码段列表值的KeyValuePair
现在,首先,如果您为您的项目获得了Newtonsoft.JSON NuGet包,那么JSON反序列化将变得容易得多。下面是您将如何实现这一点
open System.Collections.Generic
open Newtonsoft.Json
// ... All your other code here. Assuming response.Content is your json
let d = JsonConvert.DeserializeObject<Dictionary<string, List<Snippet>>(response.Content)
// And finally get your list
let items = d.["items"]
open System.Collections.Generic
打开Newtonsoft.Json
// ... 所有其他代码都在这里。假设response.Content是您的json
让d=JsonConvert.DeserializeObject不久前,我遇到了一个类似的问题,并发布了一个关于它的问题(除了我的问题是C#…我假设解决方案在F#中也有效)。问题在于,您的数据实际上是一个带有字符串键和代码段列表值的KeyValuePair
现在,首先,如果您为您的项目获得了Newtonsoft.JSON NuGet包,那么JSON反序列化将变得容易得多。下面是您将如何实现这一点
open System.Collections.Generic
open Newtonsoft.Json
// ... All your other code here. Assuming response.Content is your json
let d = JsonConvert.DeserializeObject<Dictionary<string, List<Snippet>>(response.Content)
// And finally get your list
let items = d.["items"]
open System.Collections.Generic
打开Newtonsoft.Json
// ... 所有其他代码都在这里。假设response.Content是您的json
让d=JsonConvert.DeserializeObjectThank。我试过你的建议。但是,我收到了相同的错误。谢谢。我试过你的建议。但是,我收到了相同的错误。