在C#中反序列化JSON的多个记录
本文关键字:记录 JSON 反序列化 | 更新日期: 2023-09-27 18:28:00
我有以下JSON
string json = @"[[{""campaignId"":201410018,""programCode"":""54321""},{""reason"":201410018,""about"":""54321""}],[{""campaignId"":201410019,""programCode"":""54322""},{""reason"":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);
但我的两个类数据仍然为空。
非常感谢您的帮助。
因为您的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没有您在类中显示的结构。json不是一个包含Inf
和number
类数组的响应列表,而是一个Inf
和number
类对的列表。如果您将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();
}
}
假设你负责你的数据,那么你可以改变你的结构,而不是让事情变得复杂
eg而不是
@"[[{""campaignId"":201410018,""programCode"":""54321""},{""reason"":201410018,""about"":""54321""}],[{""campaignId"":201410019,""programCode"":""54322""},{""reason"":201410018,""about"":""54321""}]]"
尝试
@"[{info:{""campaignId"":201410018,""programCode"":""54321""},number:{""reason"":201410018,""about"":""54321""}},{info:{""campaignId"":201410019,""programCode"":""54322""},number:{""reason"":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列表