简体   繁体   English

反序列化JSON C#

[英]Deserialize JSON C#

I am having a problem deserializing a json object because it does not consist of an array. 我在反序列化json对象时遇到问题,因为它不包含数组。

A sample of the json string : json字符串的示例:

 {
  "Data": {
    "A1": {
      "name": "",
      "code": "",      
      "type": ""
    },
    "A2": {
     "name": "",
      "code": "",      
      "type": ""
    },
    "A3": {
      "name": "",
      "code": "",      
      "type": ""
    }
  }
}

And here is my code, the json string will be read from a file, and cannot be changed. 这是我的代码,json字符串将从文件中读取,并且无法更改。

var json = "{\"Data\":{\"A1\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A2\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A3\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"}}}";
var jobject = JsonConvert.DeserializeObject<DataContainer>(json);


public class Data
{
    public string name { get; set; }
    public string code { get; set; }            
    public string type { get; set; }
}

public class DataContainer
{
    public List<Data> Data { get; set; }
}

Only way I have managed to do this is by changing the json to use an array like the sample below, but I would hope to solve this without having to change the format of the json file. 我设法做到这一点的唯一方法是通过将json更改为使用类似下面的示例的数组,但是我希望解决该问题而不必更改json文件的格式。

{
  "Data": [
    {
      "name": "",
      "code": "",      
      "type": ""
    },
    {
     "name": "",
      "code": "",      
      "type": ""
    },
    {
      "name": "",
      "code": "",      
      "type": ""
    }
  ]
}

Thanks 谢谢

your json contains a Data object which contains three different objects: A1, A2, A3. 您的json包含一个Data对象,该对象包含三个不同的对象:A1,A2,A3。

So I think you need to implement three different classes, and a container 所以我认为您需要实现三个不同的类和一个容器

public class A1
{
    public string name { get; set; }
    public string code { get; set; }
    public string type { get; set; }
}

public class A2
{
    public string name { get; set; }
    public string code { get; set; }
    public string type { get; set; }
}

public class A3
{
    public string name { get; set; }
    public string code { get; set; }
    public string type { get; set; }
}

public class Data
{
    public A1 A1 { get; set; }
    public A2 A2 { get; set; }
    public A3 A3 { get; set; }
}

I created 2 classes and manually deserialize the json object like this: 我创建了2个类,并手动反序列化json对象,如下所示:

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"Data\":{\"A1\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A2\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A3\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"}}}";
        var data = (JsonConvert.DeserializeObject(json) as Newtonsoft.Json.Linq.JObject).First.First;

        List<DataItem> list = new List<DataItem>();
        foreach (var item in data.ToList())
        {
            list.Add(new DataItem()
            {
                Name = ((Newtonsoft.Json.Linq.JProperty)(item)).Name,
                A = JsonConvert.DeserializeObject<A>(item.First.ToString())
            });
        }
    }

    class DataItem
    {
        public string Name { get; set; } //A1, A2, A3 ....
        public A A { get; set; }
    }

    class A
    {
        public string name { get; set; }
        public string code { get; set; }
        public string type { get; set; }
    }
}

hope this help you. 希望对您有帮助。

If you want to use your original JSON data format, change the definition of your DataContainer class to use a Dictionary<string, Data> instead of a List<Data> : 如果要使用原始JSON数据格式,请更改DataContainer类的定义以使用Dictionary<string, Data>而不是List<Data>

public class DataContainer
{
    public Dictionary<string, Data> Data { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM