将JSON数据加载到C#类[重复]

问题描述 投票:0回答:1

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

Json Data

{
    "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# Class

我基于对上面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;}
}

Converting json to class

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

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

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

Exception

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

Summary

我很欣赏一些关于理解上面的json格式的建议,以及如何成功转换它

Solution

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

var result = JsonConvert.DeserializeObject<Dictionary<string, Data>>(response.Content);
c# .net json class
1个回答
4
投票

只需复制你的json并在Visual Studio中执行Edit->Paste Special->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);
© www.soinside.com 2019 - 2024. All rights reserved.