[英]Newtonsoft Json deserialization into specific types
我有一个Action类,以及从该类派生的许多其他特定类,例如SendCoordinates,MakeCall等。
因此,给出这样的JSON响应:
{
"action":{
"type":"SendCoordinates",
"data": "41°24'12.2 N 2°10'26.5"
}
}
现在我的问题是关于Newtonsoft json库。 实施此的最佳方法是什么? 我是否应该为每个类创建特定的JSON转换器,如它们在此处显示的http://www.newtonsoft.com/json/help/html/DeserializeCustomCreationConverter.htm
还是我应该选择完全不考虑的完全不同的方法? 你们可以在这方面让我留下您的意见吗? 提前致谢
使用Newtonsoft.Json,您可以通过非泛型DeserializeObject(string value, type type)
反序列化为一个类型。
这意味着您可以使用Type
属性作为要反序列化的Type
的提示。
Action
对象的类型名称 请参见以下示例:
using System;
using Newtonsoft.Json;
namespace com.example.SO42736347
{
public class Action
{
public string Type { get; set; }
}
public class Action1 : Action
{
public string Data { get; set; }
}
public class Program
{
public const string ACTION1 = @"{
""Type"" : ""com.example.Json.Action1"",
""Data"" : ""41°24'12.2 N 2°10'26.5""
}";
public static void Main()
{
var action = JsonConvert.DeserializeObject<Action>(ACTION1);
var type = Type.GetType(action.Type);
var action1 = (Action1) JsonConvert.DeserializeObject(ACTION1, type);
}
}
}
如果您不想在“ Type
字段中指定完整的限定类型名称,则可以使用自定义程序逻辑(例如,在基础名称空间的前面加上前缀)来构造类型的FullName。
编辑 :根据意见,应该能够deserialise的(派生类)操作的列表 。 因此,我将以下示例附加到我的答案中,您可以在其中看到如何通过执行以下操作对操作列表进行反序列化:
Type
的的Action
转换后,我还添加了一个循环,以显示如何进一步处理转换后的动作。
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace com.example.Json
{
public class Action
{
public string Type { get; set; }
}
public class Action1 : Action
{
public string Data { get; set; }
}
public class Action2 : Action
{
public string SomeProperty { get; set; }
}
public class Program
{
public const string ACTION1 = @"{
""Type"" : ""com.example.Json.Action1"",
""Data"" : ""41°24'12.2 N 2°10'26.5""
}";
public const string ACTION2 = @"{
""Type"" : ""com.example.Json.Action2"",
""SomeProperty"" : ""arbitrary-value""
}";
public const string ACTIONS =
"[" +
ACTION1 +
"," +
ACTION2 +
"]" ;
public static void Main()
{
var actions = new List<Action>();
JArray jArray = JArray.Parse(ACTIONS);
foreach(var item in jArray)
{
var json = JsonConvert.SerializeObject(item);
var baseAction = JsonConvert.DeserializeObject<Action>(json);
var type = Type.GetType(baseAction.Type);
var action = (Action) JsonConvert.DeserializeObject(json, type);
actions.Add(action);
}
// now that we have converted all array items into specific derived action objects
// we can start processing them anyway we want
// keep in mind that you have to check the runtime type in order to find out what
// specific kind of action we have
// eg.
foreach(var action in actions)
{
switch(action.Type)
{
case "com.example.Json.Action1":
// do something
Console.WriteLine("found com.example.Json.Action1");
Console.WriteLine((action as Action1).Data);
break;
case "com.example.Json.Action2":
// do something
Console.WriteLine("found com.example.Json.Action2");
Console.WriteLine((action as Action2).SomeProperty);
break;
default:
// do something
Console.WriteLine("found something else");
break;
}
}
}
}
}
您可以将其反序列化为SomeObject
:
public class SomeObject
{
public SomeAction Action { get; set; }
public OtherAction Call { get; set; }
}
public class SomeAction
{
public string Type { get; set; }
public string Data { get; set; }
}
public class OtherAction { ... }
可能反序列化的json
:
{
"action":{ "type":"SendCoordinates", "data": "41°24'12.2 N 2°10'26.5" }
}
要么:
{
"call":{ ... }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.