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

时间:2014-10-26 00:24:04

标签: c# json

我有以下JSON

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

但我的两个班级数据仍为空。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

因为您的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;
}

答案 1 :(得分:0)

您的JSON没有您在课程中显示的结构。 json包含Infnumber类对的列表,而不是包含Infnumber类数组的响应列表列表。如果将json粘贴到http://jsonformatter.curiousconcept.com/,则可以看到此信息。将每个对打包到容器类中是有意义的,但不幸的是,这些对是以数组而不是作为不同的属性传输的,因此它们没有可以映射到类属性名的名称。相反,您可以编写自定义JsonConverter来解析数组并适当地分配字段:

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

答案 2 :(得分:0)

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

例如,而不是

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

@"[{info:{""campaignId"":201410018,""programCode"":""54321""},number:{""reason"":201410018,"‌​"about"":""54321""}},{info:{""campaignId"":201410019,""programCode"":""54322""},number:{""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列表来处理