Used a Jsonconverter it is possible to deserialize this json on a property of the type Dictionary<string,string>
or in a List<Item>
, with class Item stated in this way:
public class Item
{
//Note que o nome das propriedades pode ser outro qualquer.
public string Key { get;}
public string Value { get;}
public Item(string key, string value)
{
Key = key;
Value = value;
}
}
The advantage is that whatever the number of pairs "key":"value"
, the class to receive the result is always the same.
The only drawback is that whole values will have to be converted to string, which I believe is not an impediment to the use of the result.
Deserialize to Dictionary<string,string>
:
Class to receive the result of deserialization.
public class CJson
{
[JsonConverter(typeof(MapJsonConverter))]
public Dictionary<string, string> C { get; set; }
}
Json Class Converter.
public class MapJsonConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
if (!CanConvert(objectType))
{
throw new InvalidCastException(
string.Format($"Can't cast from {objectType} to {typeof (Dictionary<string, string>)}");
}
if (reader.TokenType == JsonToken.StartArray)
{
var map = new Dictionary<string, string>();
reader.Read();//Start object
reader.Read();//Object
while (reader.TokenType != JsonToken.EndArray)
{
if (reader.TokenType == JsonToken.PropertyName)
{
var key = reader.Value.ToString();
reader.Read();//Next Token
var value = reader.Value.ToString();
map.Add(key,value);
}
reader.Read();//Next Token
}
return map;
}
throw new InvalidOperationException("Unexpected Json content");
}
public override bool CanConvert(Type objectType)
{
return typeof(Dictionary<string, string>).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Note: If the set of pairs "key":"value"
had the values(value) all of the same type, the deserialization could be done directly in a property of the type List<Dictionary<string, string>>
, without the need for a Jsonconverter.
Deserialize to List<Item>
:
Class to receive the result of deserialization.
public class CJson
{
[JsonConverter(typeof(MapJsonConverter))]
public IList<Item> C { get; set; }
}
Json Class Converter.
public class MapJsonConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
if (!CanConvert(objectType))
{
throw new InvalidCastException(
string.Format($"Can't cast from {objectType} to {typeof (List<Item>)}");
}
if (reader.TokenType == JsonToken.StartArray)
{
var list = new List<Item>();
reader.Read();//Start object
reader.Read();//Object
while (reader.TokenType != JsonToken.EndArray)
{
if (reader.TokenType == JsonToken.PropertyName)
{
var key = reader.Value.ToString();
reader.Read();//Next Token
var value = reader.Value.ToString();
var item = new Item(key, value);
list.Add(item);
}
reader.Read();//Next Token
}
return list;
}
throw new InvalidOperationException("Unexpected Json content");
}
public override bool CanConvert(Type objectType)
{
return typeof(IList<Item>).IsAssignableFrom(objectType) ||
typeof(List<Item>).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
In both cases, use
CJson result = JsonConvert.DeserializeObject<CJson>(jsonString);
to deserialize.