将 JSON 数据加载到 C# 类中

Kli*_*ker 1 .net c# json class

我对 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"]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

值得注意的是,这只是 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;}
}
Run Code Online (Sandbox Code Playgroud)

将 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);
Run Code Online (Sandbox Code Playgroud)

例外

无法将当前 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);
Run Code Online (Sandbox Code Playgroud)

Vid*_*ius 7

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

    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; }
    }
Run Code Online (Sandbox Code Playgroud)

那么用法将是这样的:

        var rootObject = JsonConvert.DeserializeObject<Rootobject>(jsonString);
Run Code Online (Sandbox Code Playgroud)