gpt4 book ai didi

c# - Newtonsoft.Json.Linq.JArray' 键入 'System.Collections.Generic.IEnumerable

转载 作者:行者123 更新时间:2023-11-30 13:21:56 24 4
gpt4 key购买 nike

我正在从我的 C# 桌面客户端调用我的 Web API。

这是我在客户端的代码:

public IEnumerable<Model.Print> Get()
{
var print = new List<Model.Print>();

using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Shared.URL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Shared.HeaderType));
var response = client.GetAsync(route + "?" + GeneralTags.CUSTOMER_REF + "=" + new Guid().ToString()).Result;

if (response.IsSuccessStatusCode)
{
string strJson = response.Content.ReadAsStringAsync().Result;
var jObj2 = (IEnumerable<Model.Print>)JsonConvert.DeserializeObject(strJson);
}
else
{
evError(new Exception(String.Format("{0}: {1}", (int)response.StatusCode, response.ReasonPhrase)), ErrorTags.PRINT_GET);
}
}
return print;
}

但是我在这一行得到一个错误:

var jObj2 = (IEnumerable<Model.Print>)JsonConvert.DeserializeObject(strJson);

错误是:

'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.IEnumerable`1[InformedWorkerServer.Model.Print]'.

strJson 包含一个值:

[
{"PrintId":1,"PrintRef":"00000000-0000-0000-0000-000000000000","Header":"header","TC":"tc","CompanyRef":"00000000-0000-0000-0000-000000000000"},
{"PrintId":2,"PrintRef":"39a10cee-7cb3-4ed3-aec2-293761eed96d","Header":"header","TC":"tc","CompanyRef":"00000000-0000-0000-0000-000000000000"}]

我错过了什么?

最佳答案

使用 JsonConvert.DeserializeObject<T>相反:

var jObj2 = JsonConvert.DeserializeObject<List<Model.Print>>(strJson);

关于c# - Newtonsoft.Json.Linq.JArray' 键入 'System.Collections.Generic.IEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33462639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com