使用Newtonsoft.Json

时间:2018-01-13 14:48:24

标签: c# asp.net json asp.net-web-api json.net

我有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; }
}

相反,我想将posxposy属性反序列化为名为Position且具有XY属性的子类:

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
        }    
    ]
}

如何使此代码正常工作?

2 个答案:

答案 0 :(得分:1)

有几种方法可以达到你想要的效果 最快捷的方法是对原始Position类进行一些更改:

  1. 添加新的[JsonIgnore]媒体资源,并使用X
  2. 进行标记
  3. YPosition属性设为私有,并对其进行更改,以便他们写入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; } } } 对象并从中读取。
  4. 生成的类应如下所示:

    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]

    演示小提琴:https://dotnetfiddle.net/5zT5Rf

答案 1 :(得分:0)

如果您使用Newtonsoft,您应该能够使用Vehicle myVehicle = JsonConvert.DeserializeObject<Vechicle)(myString)进行反序列化,因为:

  • 您正在使用帖子底部的课程。
  • 您正在对从该类序列化的Json进行反序列化。如果您使用之前的Vehicle类中的旧Json不起作用,除非您专门处理该情况(如果第一次反序列化失败,请尝试使用旧类,如果成功将其转换为新的Vehicle类。