1

我有这样的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; }
}
4

1 回答 1

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;
        }
    }
}
于 2012-07-11T01:33:41.830 回答