我正在尝试通过执行以下操作将Lookup序列化为JSON:
Lookup<string, string> productIdsByCategory = prodcuts.ToLookup(p => p.Category, p => p.Id);
然后在我的ASP.NET Core WebAPI控制器中,我正在返回:
var response = new ComplexType
{
PropertyA = productIdsByCategory
... other properties left out ...
};
return Json(response)
我的WebAPI中邮递员的回答如下:
{
"PropertyA": [
[
"ProductId_A"
],
[
"ProductId_B"
],
[
"ProductId_C"
]
],
... other properties left out ...
}
密钥不包含在JSON响应中,是否可以执行此操作?
答案 0 :(得分:5)
你正好在Newtonsoft.Json没有很好地对待Lookup<TKey, TElement>
。以下代码:
var input = new List<string>()
{
"A1",
"A2",
"B1",
"C1",
"C2"
};
var lookup = input.ToLookup(i => i[0], i => i);
var json = JsonConvert.SerializeObject(lookup);
Console.WriteLine(json);
序列化为:
[["A1","A2"],["B1"],["C1","C2"]]
您正确地注意到密钥名称(A
,B
,C
)缺失,它似乎被序列化为数组数组。
似乎不支持开箱即用序列化查找,因此您必须编写自己的转换器。
我找到了一个here,但这需要您事先了解密钥和值的类型。
修改它,这就完成了工作:
// Same as above, up till here
var json = JsonConvert.SerializeObject(lookup, new LookupSerializer());
Console.WriteLine(json);
}
}
public class LookupSerializer : JsonConverter
{
public override bool CanConvert(Type objectType)
{
var result = objectType.GetInterfaces().Any(a => a.IsGenericType
&& a.GetGenericTypeDefinition() == typeof(ILookup<,>));
return result;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var obj = new JObject();
var enumerable = (IEnumerable)value;
foreach (object kvp in enumerable)
{
// TODO: caching
var keyProp = kvp.GetType().GetProperty("Key");
var keyValue = keyProp.GetValue(kvp, null);
obj.Add(keyValue.ToString(), JArray.FromObject((IEnumerable)kvp));
}
obj.WriteTo(writer);
}
}
输出:
{
"A": ["A1", "A2"],
"B": ["B1"],
"C": ["C1", "C2"]
}
答案 1 :(得分:4)
作为创建自定义转换器的一种解决方法,您可以将Lookup<TKey, TElement>
转换为Newtonsoft.Json很好玩的Dictionary<TKey, List<TElement>>
。
myLookup.ToDictionary(x => x.Key, x => x.ToList());