JSON序列化到多个属性 [英] Deserialize JSON to multiple properties
问题描述
我编程对第三方API,返回JSON数据,但格式可以是一个有些奇怪。某些属性可以是一个对象(其中包含一个Id属性),或一个字符串(这是该对象的ID)。例如,下面的两个都是有效的:
I am programming against a third party API which returns JSON data, but the format can be a little strange. Certain properties can either be an object (which contains an Id property), or a string (which is the Id of the object). For example, both of the following are valid:
{
ChildObject: 'childobjectkey1'
}
和
{
ChildObject: {
Id: 'childobjectkey1',
// (other properties)
}
}
我想这反序列化使用JSON.net成一个强类型的类,但还没有多少运气至今。我最好的想法是把它序列化到两个属性,一个是字符串,而其他的对象,并使用自定义JsonConverter每个允许变量行为:
I'm trying to deserialize this using JSON.net into a strongly typed class, but haven't had much luck so far. My best idea was to serialise it to two properties, one a string and the other an object, and to use a custom JsonConverter for each to allow for the variable behaviour:
public abstract class BaseEntity
{
public string Id { get; set; }
}
public class ChildObject : BaseEntity { }
public class MyObject
{
[JsonProperty("ChildObject")]
[JsonConverter(typeof(MyCustomIdConverter))]
public string ChildObjectId { get; set; }
[JsonProperty("ChildObject")]
[JsonConverter(typeof(MyCustomObjectConverter))]
public ChildObject ChildObject { get; set; }
}
不过,设置 JsonProperty
上具有相同属性名的两个属性属性导致异常:
However, setting the JsonProperty
attribute on two properties with the same PropertyName causes the exception:
Newtonsoft.Json.JsonSerializationException:名为$ b的成员$ b'ChildObject'已经存在于'......。
使用JsonPropertyAttribute指定另一个名字。
Newtonsoft.Json.JsonSerializationException: A member with the name 'ChildObject' already exists on '.....'. Use the JsonPropertyAttribute to specify another name.
我相当肯定的JsonConverter方法将工作,如果我能得到过这个障碍 - 我怀疑的错误是存在的,因为JsonProperty属性用于序列化和反序列化的。在这种情况下我在序列化这个类没有兴趣 - 它仅会被用作目标反序列化
I'm fairly sure the JsonConverter approach will work if I can get over this hurdle - I suspect the error is there because the JsonProperty attribute is used for Serialization as well as Deserialization. In this instance I have no interest in Serializing this class - it will only ever be used as the target for Deserialization.
我有超过远端没有控制(这是一个第三方API),但我想应该能实现这个序列化。我不介意它的使用,我已经开始对方法,或者一个我还没想到的
I have no control over the remote end (it's a third party API), but I would like to be able to achieve this serialisation. I don't mind if it's using the approach I've started on, or one I've not thought of yet.
的This问题也有关,但没有答案。
This question is also related, but there were no answers.
推荐答案
试试这个(有一些彻底的验证扩展它,如果你能在你的代码中使用它):
Try this (extend it with some thorough validation if you'll be using it in your code):
public class MyObject
{
public ChildObject MyChildObject;
public string MyChildObjectId;
[JsonProperty("ChildObject")]
public object ChildObject
{
get
{
return MyChildObject;
}
set
{
if (value is JObject)
{
MyChildObject = ((JToken)value).ToObject<ChildObject>();
MyChildObjectId = MyChildObject.Id;
}
else
{
MyChildObjectId = value.ToString();
MyChildObject = null;
}
}
}
}
这篇关于JSON序列化到多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!