我有像这样的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对象的经度和纬度属性
public class Test
{
public GeoCoordinate Location{ get; set; }
}
答案 0 :(得分:4)
这不是你提出的要求,但你可以这样定义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;
}
}
}