将两个属性反序列化为一个 .NET 属性

bla*_*jid 1 c# json.net deserialization

我有这样的 json 响应

{ latitude: 30.4848, longitude: -70.5484 }
Run Code Online (Sandbox Code Playgroud)

现在我这样做是为了用 newtonsoft JSON.NET 反序列化

JsonConvert.DeserializeObject<Test>(json);
Run Code Online (Sandbox Code Playgroud)

并反序列化为

[JsonObject(MemberSerialization.OptIn)]
public class Test
{
    [JsonProperty(PropertyName = "longitude")]
    public double Longitude{ get; set; }

    [JsonProperty(PropertyName = "latitude")]
    public double Latitude { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想将纬度和经度反序列化为 GeoCoordinate 对象的经度和纬度属性

public class Test
{
    public GeoCoordinate Location{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

ken*_*ner 5

这不是很你问什么,而是你可以定义Location这样的,而不是

[JsonObject(MemberSerialization.OptIn)]
public class Test
{
    private GeoCoordindate _location;

    [JsonProperty(PropertyName = "longitude")]
    public double Longitude{ get; set; }

    [JsonProperty(PropertyName = "latitude")]
    public double Latitude { get; set; }

    public GeoCoordinate Location
    {
        get
        {
            if (_location == null)
                _location = new GeoCoordinate(Latitude, Longitude);

            return _location;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)