gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:57:59 24 4
gpt4 key购买 nike

我有一个像这样的 json 响应

{ latitude: 30.4848, longitude: -70.5484 }

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

JsonConvert.DeserializeObject<Test>(json);

反序列化为

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

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

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

public class Test
{
public GeoCoordinate Location{ get; set; }
}

最佳答案

这与您的要求不完全相同,但您可以像这样定义 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;
}
}
}

关于c# - 将两个属性反序列化为一个 .NET 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11424301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com