【发布时间】:2020-09-26 12:54:48
【问题描述】:
我正在处理的服务返回嵌套在通用对象中的不同对象。这是来自服务的示例模型结果
{"date":1591430887.591481,"results":[{"identity":"result_type_a","result":{"attr_a":1591427634}}]}
{"date":1591430887.591481,"results":[{"identity":"result_type_b","result":{"attr_b":1591427634,"attr_bb":3591457634}}]}
{"date":1591430887.591481,"results":[{"identity":"result_type_c","result":{"attr_c":1591427634,"attr_cc":3591457634,"attr_cc":59634}},{"identity":"result_type_d","result":{"attr_d":"rfrvr","attr_dd":"ytur"}}]}
我尝试创建一个将结果属性声明为字符串的通用对象。并计划我可以将返回的 json 反序列化为该通用对象,检查身份属性并将结果属性中的字符串反序列化为特定对象类型。
这是对象结构
public class GeneralResponse
{
[JsonProperty("date")]
public double Date { get; set; }
[JsonProperty("results")]
public List<GenericResults> Results { get; set; }
}
public class GenericResults
{
[JsonProperty("identity")]
public string Identity { get; set; }
[JsonProperty("result")]
public string Result { get; set; }
}
要序列化/反序列化,我使用的是 Newtonsoft 库,代码如下
public static GeneralResponse SerializeResponse(string response)
{
return JsonConvert.DeserializeObject<GeneralResponse>(response);
}
不幸的是,我在反序列化通用对象时遇到了以下异常。
“解析值时遇到意外字符:{.Path 'results[0].result', line 1, position 71.”
如果我将 GenericResult 的 Result 属性声明为对象,如下所示
public class GenericResults
{
[JsonProperty("identity")]
public string Identity { get; set; }
[JsonProperty("result")]
public object Result { get; set; }
}
我可以通过第一次序列化并进行第二次序列化而不会出现任何异常。
string inner_object = response.Result.ToString();
switch (type)
{
case ResponseTypes.type_A: return JsonConvert.DeserializeObject<ObjectTypeA>(inner_object);
case ResponseTypes.type_B: return JsonConvert.DeserializeObject<ObjectTypeB>(inner_object);
case ResponseTypes.type_C: return JsonConvert.DeserializeObject<ObjectTypeC>(inner_object);
default: throw new Exception("Unknown Response Type");
}
但返回的对象不包含数据。 我将不胜感激有关对该算法建模的任何帮助。提前致谢。
【问题讨论】:
-
看起来您的
Result应该是多态类型层次结构。要反序列化这样的类型层次结构,请参阅 Deserializing polymorphic json classes without type information using json.net 和 How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?,这看起来是重复的。同意吗?
标签: c# json serialization json.net