问题
The problem is, that json_encode()
function in PHP leaves ambiguity for tools which are reading its output. In PHP both lists and dictionaries are same type of array
.
echo json_encode([]); // []
echo json_encode(["5" => "something"]); // {"5": "something"}
In JSON.NET I want to force both []
and {"5": "something"}
to convert to Dictionary<string, string>
type. However it recognizes []
as prohibited structure for Dictionary and throws an Exception.
Can I quickly leave empty JSON arrays nulled or force them to convert to empty Dictionary type?
FINAL SOLUTION
I modified accepted answer to make it generic and reusable for other types.
public class DictionaryOrEmptyArrayConverter<T,F> : JsonConverter
{
public override bool CanWrite { get { return false; } }
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Dictionary<T, F>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.StartArray:
reader.Read();
if (reader.TokenType == JsonToken.EndArray)
return new Dictionary<T, F>();
else
throw new JsonSerializationException("Non-empty JSON array does not make a valid Dictionary!");
case JsonToken.Null:
return null;
case JsonToken.StartObject:
var tw = new System.IO.StringWriter();
var writer = new JsonTextWriter(tw);
writer.WriteStartObject();
int initialDepth = reader.Depth;
while (reader.Read() && reader.Depth > initialDepth)
{
writer.WriteToken(reader);
}
writer.WriteEndObject();
writer.Flush();
return JsonConvert.DeserializeObject<Dictionary<T, F>>(tw.ToString());
default:
throw new JsonSerializationException("Unexpected token!");
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You should use it with your JSON object as in example below:
public class Company
{
public string industry_name { get; set; }
[JsonConverter(typeof(DictionaryOrEmptyArrayConverter<int, Upgrade>))]
public Dictionary<int, Upgrade> upgrades { get; set; }
}
public class Upgrade
{
public int level { get; set; }
}
It allows you to quickly convert JSON string to objects with DeserializeObject
method:
var result = JsonConvert.DeserializeObject<Company>(jsonString);
回答1:
You can do it with a custom JsonConverter
, although I think this solution leaves a bit to be desired:
private class DictionaryConverter : JsonConverter
{
public override bool CanWrite { get { return false; } }
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Dictionary<string, string>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
reader.Read();
if (reader.TokenType == JsonToken.EndArray)
return new Dictionary<string, string>();
else
throw new JsonSerializationException("Non-empty JSON array does not make a valid Dictionary!");
}
else if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if (reader.TokenType == JsonToken.StartObject)
{
Dictionary<string, string> ret = new Dictionary<string, string>();
reader.Read();
while (reader.TokenType != JsonToken.EndObject)
{
if (reader.TokenType != JsonToken.PropertyName)
throw new JsonSerializationException("Unexpected token!");
string key = (string)reader.Value;
reader.Read();
if (reader.TokenType != JsonToken.String)
throw new JsonSerializationException("Unexpected token!");
string value = (string)reader.Value;
ret.Add(key, value);
reader.Read();
}
return ret;
}
else
{
throw new JsonSerializationException("Unexpected token!");
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
See this code in action at https://dotnetfiddle.net/zzlzH4
回答2:
Or you could preprocess the json text by removing all those empty array properties.
//Will not work with formatted json due to whitespace
json = Regex.Replace(json, ",?\"[^\"]+\":[[]]", "");
来源:https://stackoverflow.com/questions/26725138/force-conversion-of-empty-json-array-to-dictionary-type