【问题标题】:.Net c# Error with Json : Error Converting Value.Net c# Json 错误:转换值时出错
【发布时间】: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。您还应该匿名任何数据。

标签: c# .net json .net-core


【解决方案1】:

JsonPropertyAttribute 为属性指定,JSON 文本中的名称。但在您的示例中,所有属性都有[JsonProperty("Customers")]。您的模型生成的 JSON 将是:

{
  "Customer": [{
    "Customers": "Oid value",
    "Customers": "Name value",
    "Customers": "Title value",
    ...
  }]
}

在 JSON 中,您不能按级别拥有同名的某些属性。

默认情况下,json 属性的名称是类属性的名称。 解决方案:

public class Customers
{
    public string Oid { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Kwdikos { get; set; }
    public string AFM { get; set; }
    public string Email { get; set; }
    public string DOY { get; set; }
    public string Occupation { get; set; }
    public int FPA { get; set; }
}

然后

var content = await response.Content.ReadAsStringAsync();
var customers = JsonConvert.DeserializeObject<List<Customers>>(content);

编辑: 我认为响应的内容格式不正确。 也许你可以试试:

var content = await response.Content.ReadAsStringAsync();
var customersJson = Regex.Unescape(content.Substring(1, content.Length - 2));
var customers = JsonConvert.DeserializeObject<List<Customers>>(customersJson);

【讨论】:

  • 试试var content = Regex.Unescape(await response.Content.ReadAsStringAsync());
  • 现在正在尝试。我只想告诉你,我的响应 json 在每个引号属性之前都包含反斜杠 / 这可能会导致错误吗?
  • 是的,我试过了,当我尝试它时,启动器一直停止。所以上面这行代码不起作用
  • @Alex Rida,是的,每个引号前的反斜杠不是有效的 json。这解释了为什么 json2csharp.com 给你一个糟糕的模型类。查看我的编辑以获取有效的 json。
猜你喜欢
  • 2013-03-06
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2022-11-07
  • 2019-11-18
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多