【发布时间】:2020-08-17 06:47:40
【问题描述】:
我似乎无法将 Json 转换为我想要的 List 类。
我得到了 Json 文件并将其传递给Json to C#
它生成了类:
public class Customers
{
[JsonProperty("Customers")]
public string Oid { get; set; }
[JsonProperty("Customers")]
public string Name { get; set; }
[JsonProperty("Customers")]
public string Title { get; set; }
[JsonProperty("Customers")]
public string Kwdikos { get; set; }
[JsonProperty("Customers")]
public string AFM { get; set; }
[JsonProperty("Customers")]
public string Email { get; set; }
[JsonProperty("Customers")]
public string DOY { get; set; }
[JsonProperty("Customers")]
public string Occupation { get; set; }
[JsonProperty("Customers")]
public int FPA { get; set; }
}
public class CustomersList
{
[JsonProperty("Customers")]
public List<Customers> _customersList { get; set; }
}
我正在使用代码将 Json 获取到我的 List 类,如下所示:
var content = await response.Content.ReadAsStringAsync();
var customers = JsonConvert.DeserializeObject<List<CustomersList>>(content, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
但我收到一条错误消息:
Newtonsoft.Json.JsonSerliazitaionException : '错误转换值 输入“(我的 Json 文件)”
'System.Collections.Generic.List`1[DemoProject6.CustomersList]'。小路 '',第 1 行,位置 37574。'
关于如何解决这个问题的任何想法?谢谢你的时间!!!
【问题讨论】:
-
内容变量的值是多少?
-
@SenAlexandru 该值是我使用的 Get 方法的 Json 响应。
-
恐怕我们无法回答这个问题,因为我们不知道
content的值。我们知道content是什么,但我们不知道值是什么。因此,我们只能推测可能出了什么问题(问题 100% 取决于 您的 JSON)。 -
@John 刚刚更改了标签,我知道我可以复制粘贴内容但很大。但是当我使用 json2csharp 网页时,我们对内容的外观有所了解。
-
我们可能只需要查看属性
Customers的一项,因此您不需要包含所有项,只要您提供格式正确的与原始结构相匹配的 JSON。您还应该匿名任何数据。