我的变量 inputs
定义为 List<ExpandoObject>
这是jsonList的反序列化的结果,jsonList是不同结构的对象的JSON数组:
dynamic inputs = JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonList, converter);
循环遍历它们我可以获得每个对象的目标类型,因为它们都包含 Type
的属性 ClassName
目标对象。
foreach (dynamic input in inputs)
{
// Inside that loop I can get the type
var inputType = Type.GetType(string.Format("WCFService.{0}", input.Type));
// WCFService is a namespace
// How can I convert here dynamic **input** object
// into an object of type inputType ??
}
基本上我希望在循环中将输入对象转换为 input.Type
中指定为字符串的相应类型感谢任何帮助。
修改
在for-each循环中,我想做这样的事情:
var json = JsonConvert.SerializeObject(input);
Type T = Type.GetType(string.Format("WCFService.{0}", input.Type));
T obj = JsonConvert.DeserializeObject<typeof(T)>(json); // this line fails compilation
这样obj将是一个强类型对象。 我使用json序列化来进行反序列化,这样所有json属性都会自动复制到强类型obj中。 但上面的代码没有编译,最后一行抱怨T:
找不到类型或命名空间名称'T'(你错过了吗? 使用指令或程序集引用?)
EDIT2
只是FYI,传入的jsonList具有这种结构,该数组中的每个对象可以具有不同的属性,除了Name和Type:
[
{
"Name": "PLAN-A",
"Type": "CalcInputTypes1",
"CS": 1.1111,
"CUSTOM_DATE1": "2015-05-22",
"CUSTOM_EARN1": 65500.0,
"GENDER": "Male"
},
{
"Name": "PLAN-B",
"Type": "CalcInputTypes2",
"CS": 2.22222,
"CUSTOM_DATE2": "2015-05-23",
"CUSTOM_EARN2": 12000.0,
"PROVINCE": "Ontario"
}
]
CalcInputTypes1,CalcInputTypes2和很可能CalcInputTypes3,4,5 ......是该数组中此类对象的类型......
解
感谢大家的帮助,尤其是建议使用JObject而不是ExpandoObject,这使得解决方案变得更加简单和简单: 注意:“thing”将永远不会工作,因为在这种情况下T必须在编译时知道,但我需要在运行时确定类型,因此解决方案将是这样的:
public CalcOutputTypes Calculate2(string jsonList)
{
var jobjects = JsonConvert.DeserializeObject<List<JObject>>(jsonList);
foreach (var jobject in jobjects)
{
Type runtimeType = Type.GetType(string.Format("WCFService.{0}", jobject.GetValue("TYPE")));
var input = jobject.ToObject(runtimeType); // Here we convert JObject to the defined type that just created runtime
// At this moment you have a strongly typed object "input" (CalcInputTypes1 or CalcInputTypes2 or...)
}
return new CalcOutputTypes() { STATUS = "Everything is OK !! (input was: json array of heterogeneous objects)" }; // HERE YOU RETURN CalcOutputTypes OBJECT
}
答案 0 :(得分:1)
您可以避免使用ExpandoObject
,而是直接使用LINQ to JSON,如下所示:
var query = from obj in JsonConvert.DeserializeObject<List<JObject>>(jsonList, converter)
let jType = obj["Type"]
where jType != null
let type = Type.GetType(string.Format("WCFService.{0}", (string)jType))
where type != null
where obj.Remove("Type") // Assuming this is a synthetic property added during serialization that you want to remove.
select obj.ToObject(type);
var objs = query.ToList();
如果您需要将converter
传递给每个特定的ToObject()
来电,您可以执行以下操作:
var settings = new JsonSerializerSettings();
settings.Converters.Add(converter);
var serializer = JsonSerializer.Create(settings);
var query = from obj in JsonConvert.DeserializeObject<List<JObject>>(jsonList, settings)
let jType = obj["Type"]
where jType != null
let type = Type.GetType(string.Format("WCFService.{0}", (string)jType))
where type != null
where obj.Remove("Type") // Assuming this is a synthetic property added during serialization that you want to remove.
select obj.ToObject(type, serializer);
var objs = query.ToList();
答案 1 :(得分:1)
另一种可能的解决方案是在注释中建议@dbc使用Newtonsoft.Json方法JsonConvert.DeserializeObject(json, type)
,如下所示:
private T convertTo<T>(string json)
{
return (T)JsonConvert.DeserializeObject(json, typeof(T));
}
var json = // some serialized json ...
var o = convertTo<MyCustomType>(json);
其中MyCustomType
是您输入的类型(var inputType = Type.GetType(string.Format("WCFService.{0}", input.Type));
。
不要忘记隐式演员(T)
!
另一种可能性是使用.NET框架内置TypeConverter class编写自己的类型转换器。
答案 2 :(得分:0)
你不需要ExpandoObjext的列表,只需像Deserializing heterogenous JSON array into covariant List<> using JSON.NET中所描述的那样使用CustomCreationConverter,所以@JimSan的所有信用
public class Example
{
[Test]
public void Test()
{
var json =
"[\r\n {\r\n \"Name\": \"PLAN-A\",\r\n \"Type\": \"CalcInputTypes1\",\r\n \"CS\": 1.1111,\r\n \"CUSTOM_DATE1\": \"2015-05-22\",\r\n \"CUSTOM_EARN1\": 65500.0,\r\n \"GENDER\": \"Male\"\r\n },\r\n {\r\n \"Name\": \"PLAN-B\",\r\n \"Type\": \"CalcInputTypes2\",\r\n \"CS\": 2.22222,\r\n \"CUSTOM_DATE2\": \"2015-05-23\",\r\n \"CUSTOM_EARN2\": 12000.0,\r\n \"PROVINCE\": \"Ontario\"\r\n }\r\n]";
var result = JsonConvert.DeserializeObject<List<Item>>(json, new JsonItemConverter());
Assert.That(result[0], Is.TypeOf<CalcInputTypes1>());
Assert.That(((CalcInputTypes1)result[0]).Gender, Is.EqualTo("Male"));
Assert.That(result[1], Is.TypeOf<CalcInputTypes2>());
Assert.That(((CalcInputTypes2)result[1]).Province, Is.EqualTo("Ontario"));
}
public class JsonItemConverter : Newtonsoft.Json.Converters.CustomCreationConverter<Item>
{
public override Item Create(Type objectType)
{
throw new NotImplementedException();
}
public Item Create(Type objectType, JObject jObject)
{
var type = (string)jObject.Property("Type");
switch (type)
{
case "CalcInputTypes1":
return new CalcInputTypes1();
case "CalcInputTypes2":
return new CalcInputTypes2();
}
throw new ApplicationException(String.Format("The given type {0} is not supported!", type));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Load JObject from stream
var jObject = JObject.Load(reader);
// Create target object based on JObject
var target = Create(objectType, jObject);
// Populate the object properties
serializer.Populate(jObject.CreateReader(), target);
return target;
}
}
public abstract class Item
{
public string Type { get; set; }
}
public class CalcInputTypes1 : Item
{
[JsonProperty("GENDER")]
public string Gender { get; set; }
}
public class CalcInputTypes2 : Item
{
[JsonProperty("PROVINCE")]
public string Province { get; set; }
}
}