Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
C# 反序列化C中JSON的多条记录#_C#_Json - Fatal编程技术网

C# 反序列化C中JSON的多条记录#

C# 反序列化C中JSON的多条记录#,c#,json,C#,Json,我有以下几个问题 string json = @"[[{""campaignId"":201410018,""programCode"":""54321""},{""reason"":201410018,"‌​"about"":""54321""}],[{""campaignId"":201410019,""programCode"":""54322""},{""rea‌​son"":201410018,""about"":""54321""}]]" 我创建了以下类 public class

我有以下几个问题

string json = @"[[{""campaignId"":201410018,""programCode"":""54321""},{""reason"":201410018,"‌​"about"":""54321""}],[{""campaignId"":201410019,""programCode"":""54322""},{""rea‌​son"":201410018,""about"":""54321""}]]"
我创建了以下类

public class JSONResponse
    {

        public number[] number{ get; set; }
        public Inf[] inf{ get; set; }
    }

    public class number
    {

        public int reason { get; set; }
        public string about { get; set; }

    }
    public class Inf
    {

        public int campaignId { get; set; }
        public string programCode { get; set; }
    }
为了反序列化,我调用下面的代码

List<List<JSONResponse>> myDeserializedObjList = JsonConvert.DeserializeObject<List<List<JSONResponse>>>(jsonstr);
List myDeserializedObjList=JsonConvert.DeserializeObject(jsonstr);
但我的两个类数据仍然是空的


非常感谢您的帮助。

因为您的JSON在同一个数组中有两种不同的对象类型,所以您无法使用典型且简单的方法将其反序列化为对象。除非您可以控制JSON将结构更改为在同一数组中没有Inf和number对象的结构,否则您唯一的选择就是为什么要使用Expando/dynamic对象。下面的代码将提供的JSON解析为现有的类结构

static List<JSONResponse> Parse(string json)
{
    var responses = new List<JSONResponse>();
    //string)obj[0][0].programCode.Value;
    dynamic obj = JsonConvert.DeserializeObject<dynamic>(json);
    for (int i = 0; i < obj.Count; i++)
    {
        //responses[i] = new JSONResponse() {
        var inf = new List<Inf>();
        var numbers = new List<number>();
        for (int j = 0; j < obj[i].Count; j++)
        {
            if (obj[i][j].campaignId != null)
                inf.Add(new Inf()
                    {
                        campaignId = (int) obj[i][j].campaignId.Value,
                        programCode = obj[i][j].programCode.Value
                    });
            if (obj[i][j].reason != null)
                numbers.Add(new number()
                    {
                        about = obj[i][j].about.Value,
                        reason = (int)obj[i][j].reason.Value
                    });
        }
        responses.Add(new JSONResponse()
            {
                number = numbers.ToArray(),
                inf = inf.ToArray()
            });

    }
    return responses;
}
静态列表解析(字符串json)
{
var responses=新列表();
//字符串)obj[0][0].programCode.Value;
dynamic obj=JsonConvert.DeserializeObject(json);
for(int i=0;i
您的JSON没有您在类中显示的结构。您的JSON不是包含
Inf
number
类数组的响应列表,而是包含
Inf
number
类对的列表。如果将JSON粘贴到中,您可以看到这一点。包e是有意义的ach对转换为容器类,但不幸的是,这些对是在数组中传输的,而不是作为不同的属性传输的,因此它们没有可以映射到类属性名称的名称。相反,您可以编写一个自定义来解析数组并适当地分配字段:

public class number
{
    public int reason { get; set; }
    public string about { get; set; }
}

public class Inf
{
    public int campaignId { get; set; }
    public string programCode { get; set; }
}

public class JSONResponse
{
    public number number { get; set; }
    public Inf Inf { get; set; }

    public static List<JSONResponse> DeserializeList(string jsonstr)
    {
        return JsonConvert.DeserializeObject<List<JSONResponse>>(jsonstr, new JSONResponseConverter());
    }
}

class JSONResponseConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(JSONResponse).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader,
        Type objectType, object existingValue, JsonSerializer serializer)
    {
        JSONResponse response = new JSONResponse();
        var array = JArray.Load(reader);
        if (array.Count != 2)
        {
            // Or maybe throw an exception?
            Debug.WriteLine("Unexpected array length for " + array.ToString());
        }
        foreach (var entry in array)
        {
            if (entry["campaignId"] != null)
            {
                response.Inf = entry.ToObject<Inf>();
            }
            else if (entry["reason"] != null)
            {
                response.number = entry.ToObject<number>();
            }
            else
            {
                // Or maybe throw an exception?
                Debug.WriteLine("Unknown entry " + entry.ToString());
            }
        }
        return response;
    }

    public override void WriteJson(JsonWriter writer,
        object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
公共类编号
{
公共int原因{get;set;}
关于{get;set;}的公共字符串
}
公共类Inf
{
公共ID{get;set;}
公共字符串程序代码{get;set;}
}
公共类JSONResponse
{
公共编号{get;set;}
公共Inf{get;set;}
公共静态列表反序列化列表(字符串jsonstr)
{
返回JsonConvert.DeserializeObject(jsonstr,新的JSONResponseConverter());
}
}
类JSONResponseConverter:JsonConverter
{
公共覆盖布尔CanConvert(类型objectType)
{
返回typeof(JSONResponse).IsAssignableFrom(objectType);
}
公共重写对象ReadJson(JsonReader,
类型objectType、对象existingValue、JsonSerializer序列化程序)
{
JSONResponse=新的JSONResponse();
var数组=JArray.Load(读卡器);
如果(array.Count!=2)
{
//或者抛出一个异常?
Debug.WriteLine(“意外的“+array.ToString()数组长度”);
}
foreach(数组中的var条目)
{
如果(条目[“活动ID”]!=null)
{
response.Inf=entry.ToObject();
}
else if(条目[“原因”]!=null)
{
response.number=entry.ToObject();
}
其他的
{
//或者抛出一个异常?
Debug.WriteLine(“未知条目”+entry.ToString());
}
}
返回响应;
}
public override void WriteJson(JsonWriter writer,
对象值,JsonSerializer序列化程序)
{
抛出新的NotImplementedException();
}
}

假设您负责您的数据,那么您可以改变您的结构,而不是让事情变得复杂

代替

@"[[{""campaignId"":201410018,""programCode"":""54321""},{""reason"":201410018,"‌​"about"":""54321""}],[{""campaignId"":201410019,""programCode"":""54322""},{""rea‌​son"":201410018,""about"":""54321""}]]"
试一试

要处理这个问题呢

public class JSONResponse
{
    public number number{ get; set; }
    public Inf info{ get; set; }
}

public class number
{

    public int reason { get; set; }
    public string about { get; set; }

}
public class Inf
{

    public int campaignId { get; set; }
    public string programCode { get; set; }
}
现在您只有一个要处理的JSONResponse列表

public class JSONResponse
{
    public number number{ get; set; }
    public Inf info{ get; set; }
}

public class number
{

    public int reason { get; set; }
    public string about { get; set; }

}
public class Inf
{

    public int campaignId { get; set; }
    public string programCode { get; set; }
}