2017-05-03 70 views
2

我想用Newtonsoft創建一個JSON對象。一切看起來不錯,但我不能在空數組中創建空數組。我需要下面的輸出...C#Newtonsoft JArray。 JSON不能在空數組中創建空數組

我的代碼:

JObject rss = new JObject(
    new JProperty("query", 
    new JObject(
     new JProperty("aoi", 
     new JObject(
      new JProperty("type", "Polygon"), 
      new JProperty("coordinates", 
       new JArray(
       new JArray(
        new JArray(
        new JValue(-122.62664794921874), 
         new JValue(38.81403111409755) 
        ), 
        new JArray(
         new JValue(-122.62664794921874), 
         new JValue(38.81403111409755) 
        ) 
       ) 
       ) 
      ) 
      ) 
     ) 
     ) 
    ) 
    ); 

我能得到什麼:

{ 
    "query": { 
    "aoi": { 
     "type": "Polygon", 
     "coordinates": [ 
     [ -122.62664794921874, 38.81403111409755 ], 
     [ -122.62664794921874, 39.07464374293249 ] 
     ] 
    } 
    } 
} 

我需要什麼:

{ 
    "query": { 
    "aoi": { 
     "type": "Polygon", 
     "coordinates": [ 
     [ 
      [ -122.62664794921874, 38.81403111409755 ], 
      [ -122.62664794921874, 39.07464374293249 ] 
     ] 
     ] 
    } 
    } 
} 

在此先感謝

+0

如何使用標準的.NET對象並對其進行序列化? –

回答

1

作爲JArray構造函數的參數的單個JArray解釋爲應該複製到新的JArray的內容。它適用於如果你這樣做:

JObject rss = new JObject(
     new JProperty("query", 
      new JObject(
       new JProperty("aoi", 
        new JObject(
         new JProperty("type", "Polygon"), 
         new JProperty("coordinates", 
          new JArray(
           new JArray(
            new JArray(
             new JValue(-122.62664794921874), 
             new JValue(38.81403111409755) 
             ), 
            new JArray(
             new JValue(-122.62664794921874), 
             new JValue(38.81403111409755) 
             ) 
           ) as Object 
         )) 
         )) 
       )) 
     );   

這使得構造函數看到它應該被插入,而不是複製。

+0

謝謝!完善。 – Optimist

+0

@優惠券商,說「謝謝」的最好方式就是接受解決問題的答案。 –