Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反序列化JSON C#_C#_Json_Json.net - Fatal编程技术网

反序列化JSON C#

反序列化JSON C#,c#,json,json.net,C#,Json,Json.net,我在反序列化json对象时遇到问题,因为它不包含数组 json字符串的示例: { "Data": { "A1": { "name": "", "code": "", "type": "" }, "A2": { "name": "", "code": "", "type": "" }, "A3": { "name": "", "code

我在反序列化json对象时遇到问题,因为它不包含数组

json字符串的示例:

 {
  "Data": {
    "A1": {
      "name": "",
      "code": "",      
      "type": ""
    },
    "A2": {
     "name": "",
      "code": "",      
      "type": ""
    },
    "A3": {
      "name": "",
      "code": "",      
      "type": ""
    }
  }
}
这是我的代码,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; }
}

谢谢

您的json包含一个数据对象,其中包含三个不同的对象:A1、A2、A3

所以我认为您需要实现三个不同的类和一个容器

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; }
}

我创建了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; }
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var json=“{\'Data\”:{\'A1\”:{\'name\”:\“\”,\“code\”:\“\”,\“type\”:\“\”,\“A2\”:{\'name\”:“\”,“code\”:“\”,“type\”:“\”,“A3\”:{\'name\”:“,“code\”:“\”,“type\”:“\”,“type\”:“\”,“type\”:“\”;
var data=(JsonConvert.DeserializeObject(json)为Newtonsoft.json.Linq.JObject);
列表=新列表();
foreach(data.ToList()中的变量项)
{
添加(新数据项()
{
Name=((Newtonsoft.Json.Linq.JProperty)(item)).Name,
A=JsonConvert.DeserializeObject(item.First.ToString())
});
}
}
类数据项
{
公共字符串名称{get;set;}//A1、A2、A3。。。。
公共A{get;set;}
}
甲级
{
公共字符串名称{get;set;}
公共字符串代码{get;set;}
公共字符串类型{get;set;}
}
}

希望这对您有所帮助。

如果您想使用原始JSON数据格式,请将
数据容器
类的定义更改为使用
字典而不是
列表

公共类数据容器
{
公共字典数据{get;set;}
}

担心会有数百个具有相同结构的对象,希望在不为每个对象创建类的情况下实现这一点。谢谢,我只是想避免与设置json文件的另一方进行冗长的讨论。顺便说一句,我建议使用类似于生成c#的工具classes@BartKooi您不需要为每个项目单独设置一个类;只需使用一个类来存储数据,并使用一个字典来保存它们。看看我的答案。
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; }
    }
}
public class DataContainer
{
    public Dictionary<string, Data> Data { get; set; }
}