2012-07-11 69 views
1

我有这样反序列化两个属性到一个.NET财产

{ latitude: 30.4848, longitude: -70.5484 } 

JSON响应现在我在做这与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对象的经度和纬度属性ct

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

任何想法如何做到这一点?

感谢

+0

我喜欢@qntmfred建议+1。这个问题使我相信你会为一个简单的应用程序添加不必要的复杂性 – 2012-07-11 02:34:42

+0

你是对的,我直到@qntmfred扩展它时才理解这个建议。谢谢 – blackjid 2012-07-11 02:38:13

回答

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; 
     } 
    } 
} 
+0

我已经添加了几行试图更好地解释自己 – blackjid 2012-07-11 01:44:33

相关问题