我有JSON格式的字符串:
{
"hr": "12:56",
"vs": [
{
"pre": "73833",
"a": true,
"posx": "-46.688962875",
"posy": "-23.632046625"
},
{
"pre": "73722",
"a": true,
"posx": "-46.773388125",
"posy": "-23.68939025"
}
]
}
我想将JSON反序列化为此类:
public class Tracking
{
[JsonProperty(PropertyName = "hr")]
public string Hour { get; set; }
[JsonProperty(PropertyName = "vs")]
public IList<Vehicle> Vehicles { get; set; }
}
Vehicle
类目前具有以下属性:
public class Vehicle
{
[JsonProperty (PropertyName = "pre")]
public string Prefix {get; set; }
[JsonProperty (PropertyName = "posx")]
public string Y {get; set; }
[JsonProperty (PropertyName = "posy")]
public string X {get; set; }
}
相反,我想将posx
和posy
属性反序列化为名为Position
且具有X
和Y
属性的子类:
public class Position
{
[JsonProperty (PropertyName = "posx")]
public string Y {get; set; }
[JsonProperty (PropertyName = "posy")]
public string X {get; set; }
}
导致以下Vehicle
类:
public class Vehicle
{
[JsonProperty (PropertyName = "pre")]
public string Prefix {get; set; }
public Position Position {get; set; }
}
我打算在我的系统上的更多地方使用Position
类,并且不想重复这些属性。但是在使用新结构进行反序列化时,Position
属性将返回null。
{
"hr": "13:08",
"vs": [
{
"pre": "73833",
"a": true,
"Position": null
},
{
"pre": "73722",
"a": true,
"Position": null
}
]
}
如何使此代码正常工作?
答案 0 :(得分:1)
有几种方法可以达到你想要的效果
最快捷的方法是对原始Position
类进行一些更改:
[JsonIgnore]
媒体资源,并使用X
Y
和Position
属性设为私有,并对其进行更改,以便他们写入public class Vehicle
{
[JsonProperty(PropertyName = "pre")]
public string Prefix { get; set; }
[JsonIgnore]
public Position Position { get; set; }
[JsonProperty(PropertyName = "posx")]
private string Y
{
get { return Position != null ? Position.Y : null; }
set
{
if (Position == null) { Position = new Position(); }
Position.Y = value;
}
}
[JsonProperty(PropertyName = "posy")]
private string X
{
get { return Position != null ? Position.X : null; }
set
{
if (Position == null) { Position = new Position(); }
Position.X = value;
}
}
}
对象并从中读取。生成的类应如下所示:
JsonConverter
演示小提琴:Format current date and time
如果您不喜欢这个想法,另一个选择是为您的Vehicle
课程制作自定义class VehicleConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Vehicle);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject obj = JObject.Load(reader);
Vehicle veh = new Vehicle();
serializer.Populate(obj.CreateReader(), veh);
Position pos = new Position();
serializer.Populate(obj.CreateReader(), pos);
veh.Position = pos;
return veh;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Vehicle veh = (Vehicle)value;
JObject obj = JObject.FromObject(veh.Position, serializer);
obj.AddFirst(new JProperty("pre", veh.Prefix));
obj.WriteTo(writer);
}
}
:
Vehicle
然后,使用[JsonConverter]
属性标记您的[JsonConverter(typeof(VehicleConverter))]
public class Vehicle
{
[JsonProperty(PropertyName = "pre")]
public string Prefix { get; set; }
public Position Position { get; set; }
}
类,将其与转换器绑定:
a = [3,5,2,3,1]
答案 1 :(得分:0)
如果您使用Newtonsoft,您应该能够使用Vehicle myVehicle = JsonConvert.DeserializeObject<Vechicle)(myString)
进行反序列化,因为: