2014-06-13 64 views
1

我試圖反序列化GeoJson,所以我可以將其分解並存儲在Db中。當我嘗試反序列化它時,coordinates的反序列化失敗。如何反序列化嵌套的JSON數組?

我使用下面的類幾何:

public class GeoJsonGeometry { 
    public string type { get; set; } 
    public string[, ,] coordinates { get; set; } 
} 

"geometry": { "type": "Polygon", "coordinates": [ [ [ -85.388717, 33.913044 ], [ -85.380885, 33.873508 ], [ -85.379455, 33.866291 ], [ -85.377426, 33.856047 ], [ -85.376403, 33.850656 ], [ -85.364595, 33.788446 ], [ -85.361844, 33.773951 ], [ -85.360491, 33.767958 ], [ -85.357402, 33.750104 ], [ -85.355252, 33.739245 ], [ -85.344054, 33.682684 ], [ -85.342722, 33.675953 ], [ -85.323792, 33.580339 ], [ -85.315340, 33.537646 ], [ -85.314994, 33.535898 ], [ -85.314843, 33.534951 ], [ -85.314091, 33.530218 ], [ -85.313999, 33.529807 ], [ -85.304439, 33.482884 ], [ -85.308211, 33.481579 ], [ -85.309250, 33.483137 ], [ -85.314852, 33.487603 ],...]]]

我試過double [,,]但它也不能工作。

我很困惑,因爲它看起來應該很好地序列化,因爲它是嵌套數組,但事實並非如此。任何幫助,將不勝感激。

我也試過List<List<List<double>>>double[][][],它總是失敗。

+1

不能理解爲什麼這個問題被拒絕投票,我有同樣的問題,這個職位幫助。 – Luther

回答

3

Geometry對象應該是

public class Geometry 
{ 
    public string type { get; set; } 
    public List<List<List<double>>> coordinates { get; set; } 
} 

編輯

var obj = JsonConvert.DeserializeObject<RootObject>(json); 


public class Geometry 
{ 
    public string type { get; set; } 
    public List<List<List<double>>> coordinates { get; set; } 
} 

public class RootObject 
{ 
    public Geometry geometry { get; set; } 
} 

JSON:

{ 
    "geometry": { 
     "type": "Polygon", 
     "coordinates": [ 
      [ 
       [ 
        -85.388717, 
        33.913044 
       ], 
       [ 
        -85.380885, 
        33.873508 
       ], 
       [ 
        -85.379455, 
        33.866291 
       ], 
       [ 
        -85.377426, 
        33.856047 
       ], 
       [ 
        -85.376403, 
        33.850656 
       ], 
       [ 
        -85.364595, 
        33.788446 
       ], 
       [ 
        -85.361844, 
        33.773951 
       ], 
       [ 
        -85.360491, 
        33.767958 
       ], 
       [ 
        -85.357402, 
        33.750104 
       ], 
       [ 
        -85.355252, 
        33.739245 
       ], 
       [ 
        -85.344054, 
        33.682684 
       ], 
       [ 
        -85.342722, 
        33.675953 
       ], 
       [ 
        -85.323792, 
        33.580339 
       ], 
       [ 
        -85.31534, 
        33.537646 
       ], 
       [ 
        -85.314994, 
        33.535898 
       ], 
       [ 
        -85.314843, 
        33.534951 
       ], 
       [ 
        -85.314091, 
        33.530218 
       ], 
       [ 
        -85.313999, 
        33.529807 
       ], 
       [ 
        -85.304439, 
        33.482884 
       ], 
       [ 
        -85.308211, 
        33.481579 
       ], 
       [ 
        -85.30925, 
        33.483137 
       ], 
       [ 
        -85.314852, 
        33.487603 
       ] 
      ] 
     ] 
    } 
} 
+0

嘗試這最初,得到:無法反序列化當前的JSON數組(例如[1,2,3])類型'System.Double',因爲該類型需要一個JSON原始值(例如字符串,數字,布爾值,空)來反序列化正確。 要修復此錯誤,請將JSON更改爲JSON原始值(例如,字符串,數字,布爾值,空值)或將反序列化類型更改爲實現集合接口(例如ICollection,IList)的數組或類型,如列表可以從JSON數組中反序列化。 JsonArrayAttribute也可以添加到類型中,以強制它從JSON數組反序列化。 – Yatrix

+0

然後我用字符串嘗試了它,但也失敗了。 – Yatrix

+0

@Yatrix我使用*部分*正確的json測試了它,它可以工作。如果你發佈**正確和完整的** json,我們可以更好地回答。 –

2

JSON不支持多維陣列。

這是一個數組數組:double[][][]

+0

嗯,我也試過,失敗了。 – Yatrix