【问题标题】:Load JSON data into a C# Class [duplicate]将 JSON 数据加载到 C# 类中[重复]
【发布时间】:2018-03-18 16:22:06
【问题描述】:

我对 json 很陌生,我正在尝试将一些 json 内容转换为 C# 类以存储它以供以后使用。我在尝试创建适合数据结构的类时遇到问题。 json内容的示例如下所示。

Json 数据

{
    "FirstItem": {
        "id": 1,
        "type": "foo",
        "colours": ["blue", "black", "green"],
        "reviews": {
            "positive": ["The best", "unbelievable", "Awesome"],
            "negative": ["Sh*t", "Awful", "Dire", "Terrible", "Appalling"],
            "neutral": ["OK", "Meh"]
        }
    },
    "SecondItem": {
        "id": 2,
        "type": "bar",
        "colours": ["red", "white", "yellow"],
        "reviews": {
            "positive": ["Great", "Amazing", "Fantastic", "Perfect", "Uplifting"],
            "negative": ["Terrible", "Shocking", "abysmal"],
            "neutral": ["OK", "Standard", "Vanilla"]
        }
    }
}

编辑:

值得注意的是,这只是 json 数据的一小部分样本,我希望使用更大的数据集,该数据集始终采用这种格式,但是 FirstItemSecondItem(名称?)总会有所不同。我可能最终得到MillionthItem

C#类

基于我对上述 json 数据的有限理解,我创建了一个类。我认为这就是问题所在——我认为这个类不适合基于上面的 json。这就是我所拥有的。

public class Data
{
    public string name {get; set;}
    public int id {get; set;}
    public string type {get; set;}
    public string[] colours {get; set;}
    public Reviews reviews {get; set;}
}

public class Reviews
{
    public string[] positive {get; set;}
    public string[] negative {get; set;}
    public string[] neutral {get; set;}
}

将 json 转换为类

为了尝试转换它,我使用JsonConvert 将数据反序列化为我创建的类的列表。例如:

var client = new RestClient(url);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);

var result = JsonConvert.DeserializeObject<List<Data>>(response.Content);

异常

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[namespace.class]' 因为该类型需要 JSON 数组(例如 [1 ,2,3]) 以正确反序列化。 要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。也可以将 JsonObjectAttribute 添加到类型中以强制它从 JSON 对象反序列化。

总结

对于理解上面的 json 格式以及如何成功转换它的一些建议,我将不胜感激

解决方案

dbc 建议将此信息存储在字典中,这正是我需要做的 - 这允许我捕获名称(FirstItemSecondItem 等)作为键和类 Data 的其余部分作为值。

var result = JsonConvert.DeserializeObject<Dictionary<string, Data>>(response.Content);

【问题讨论】:

标签: c# .net json class


【解决方案1】:

只需复制您的 json 并在 Visual Studio 中执行 Edit-&gt;Paste Special-&gt;Paste Json As Classes - 它会为您创建匹配的类结构。

    public class Rootobject
    {
        public Firstitem FirstItem { get; set; }
        public Seconditem SecondItem { get; set; }
    }

    public class Firstitem
    {
        public int id { get; set; }
        public string type { get; set; }
        public string[] colours { get; set; }
        public Reviews reviews { get; set; }
    }

    public class Reviews
    {
        public string[] positive { get; set; }
        public string[] negative { get; set; }
        public string[] neutral { get; set; }
    }

    public class Seconditem
    {
        public int id { get; set; }
        public string type { get; set; }
        public string[] colours { get; set; }
        public Reviews1 reviews { get; set; }
    }

    public class Reviews1
    {
        public string[] positive { get; set; }
        public string[] negative { get; set; }
        public string[] neutral { get; set; }
    }

那么用法会是这样的:

        var rootObject = JsonConvert.DeserializeObject<Rootobject>(jsonString);

【讨论】:

  • 我从来不知道这存在,感谢您指出这一点!使用它的问题是每个项目都必须存在一个类,我期望的不仅仅是FirstItemSecondItem。我已经更新了我的帖子以反映这一点。
  • 没问题,但是你的 Json 结构不正确 - 你真的在寻找 Items 数组,所以你的 json 应该以[ 开头并以] 结尾。如果您可以更改结构,那么您可以将JsonArray 属性添加到FirstItem 类并直接反序列化为List&lt;FirstItem&gt;(可能应该将类重命名为Item
猜你喜欢
  • 1970-01-01
  • 2020-08-29
  • 2019-01-20
  • 1970-01-01
  • 2021-11-17
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
相关资源
最近更新 更多