2012-07-11 70 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

相關問題