在c#中迭代JSON对象数组名称值对 [英] Iterating a JSON object array name value pair in c#
问题描述
我在这里提到了一个类似的问题:
I've taken this example from a similar question here:
但是我的数据格式有些不同,它有数组括号。
But my data format is a little different, it has array brackets.
正如我所指出的那样,我可以有一个未定义的条目数量,根据定义的类将数据结构提取为数组/对象列表。
As pointed out I can have an undefined number of entries, I want to walk through the entries and extract the data structure into an array/list of objects based on the class defined.
{
"-KeArK3V02mXYWx2OMWh" : [{
"Description" : "this is a description",
"Location" : "Atlanta",
"Name" : "Event One",
"Time" : "2017-03-01T21:53:12.924645Z"
}],
"-KeAtCNF_rmewZ_U3PpH" : [{
"Description" : "another description",
"Location" : "Charlotte",
"Name" : "Event Two",
"Time" : "2017-03-01T22:01:25.603547Z"
}],
"-KeAtd8CQW_EfH3Sw4YQ" : [{
"Description" : "description goes here",
"Location" : "Toronto",
"Name" : "Event Three",
"Time" : "2017-03-01T22:03:19.3953859Z"
}]
}
我有一个名为Event的类,定义如下
and I have a class called Event that is defined as follows
class Title {
public string Description { get; set; }
public string Location { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
}
给我的答案(我认为是正确的)不工作
The answer given there (which I thought was correct) isn't working
Dictionary<string, Title> elist = JsonConvert.DeserializeObject<Dictionary<string, Title>>(jsonString);
它给我一个错误:
无法将当前JSON数组(例如[1,2,3])反序列化为标题类型,因为该类型需要一个JSON对象(例如{name:value})来正确反序列化。
要解决此错误,请将JSON更改为JSON对象(例如{name:value}),或将反序列化类型更改为实现集合接口的数组或类型(例如ICollection,IList)像可以从JSON数组反序列化的List。 JsonArrayAttribute也可以添加到类型,以强制它从JSON数组反序列化。
路径'-KeArK3V02mXYWx2OMWh',第2行,位置XX。
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Title' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '-KeArK3V02mXYWx2OMWh', line 2, position XX.
推荐答案
是Json的结构问题。你的json格式与Key和value一样,但是值是根据json格式的数组。你必须做以下事情。
There is a problem with structure of Json. Your json has format like Key and value but value is array as per json format. You have to do following thing.
Dictionary<string,Title[]> elist = JsonConvert.DeserializeObject<Dictionary<string, Title[]>>(jsonString);
以上解决方案是最简单的。
Above solution is simplest one.
第二个解决方案。 (您必须根据需要进行修改,但目前的json将会工作)
Second solution. ( You have to modify as your need but current json will work)
public class TitleConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray jObject = JArray.Load(reader);
Title title = JsonConvert.DeserializeObject<Title>(jObject[0].ToString());
return title;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
if (objectType.Name == "Title")
return true;
return false;
}
}
现在使用这样。
Dictionary<string,Title> elist = JsonConvert.DeserializeObject<Dictionary<string, Title>>(jsonString,new TitleConverter());
这篇关于在c#中迭代JSON对象数组名称值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!