2016-06-13 61 views
1

我想從API服務反序列化對象數組。.NET C#JsonConvert.DeserializeObject數組

來自API的數據具有以下形式:

{ 
"id": "9286", 
"nome": "Bairro Novo", 
"num_lotes": "312", 
"num_quadras": "9", 
"plots_id": "159351", 
"geo_data": [ 
    { 
    "loteamentos_id": "9286", 
    "G": "-7.27087569384820000000", 
    "K": "-34.90980863571200000000", 
    "index": "0", 
    "code": "V" 
    }, 
    { 
    "loteamentos_id": "9286", 
    "G": "-7.27234968660550000000", 
    "K": "-34.90971207618700000000", 
    "index": "1", 
    "code": "V" 
    }, 
    { 
    "loteamentos_id": "9286", 
    "G": "-7.27317448188540000000", 
    "K": "-34.90458905696900000000", 
    "index": "2", 
    "code": "V" 
    }, 
    { 
    "loteamentos_id": "9286", 
    "G": "-7.27176434710060000000", 
    "K": "-34.90472316741900000000", 
    "index": "3", 
    "code": "V" 
    }, 
    { 
    "loteamentos_id": "9286", 
    "G": "-7.27202508786680000000", 
    "K": "-34.90719884634000000000", 
    "index": "15", 
    "code": "C" 
    } 
] 
    }, 

類Loteamento:

public class Loteamento { 

    [JsonProperty("id")] 
    public string id { get; set; } 

    [JsonProperty("nome")] 
    public string nome { get; set; } 

    [JsonProperty("num_lotes")] 
    public string num_lotes { get; set; } 

    [JsonProperty("num_quadras")] 
    public string num_quadras { get; set; } 

    [JsonProperty("plots_id")] 
    public string plots_id { get; set; } 

    [JsonProperty("geo_data")] 
    public List<GeoData> geo_data { get; set; } 

} 

和類地理數據:

public class GeoData { 

    [JsonProperty("loteamentos_id")] 
    public string loteamentos_id { get; set; } 

    [JsonProperty("G")] 
    public string G { get; set; } 

    [JsonProperty("K")] 
    public string K { get; set; } 

    [JsonProperty("index")] 
    public string index { get; set; } 

    [JsonProperty("code")] 
    public string code { get; set; } 

} 

的問題是,我得到一個錯誤,不知道如何獲得一個數組。

在我的代碼有:

List<Loteamento>[] loteamentos = null; 
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result); 

有什麼不對?

+3

「我得到一個錯誤」 ...... *什麼錯誤*? –

+0

另外,爲什麼你想分配一個列表到一個列表數組? –

+0

@PatrickHofman Upsss!你是對的。這是問題!謝謝。 – Apalabrados

回答

1
List<Loteamento>[] loteamentos = null; 
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result); 

你的第一行聲明loteamentos爲陣列,其中該陣列中的每個小區是List<Loteamento>。因此,該變量設置爲List<Loteamento>類型的多個實例。然後

你的第二線deserialises的List<Loteamento>一個單個實例,然後嘗試分配到loteamentos變量此。該變量不適合,因爲它是一個列表數組,而不僅僅是一個列表。

我懷疑它可能會工作,如果你只是從你的第一行刪除[]

0

使用Newtonsoft Json.Net你可以反序列化這樣的:

void Main() 
{ 
    string json = @"{ 
     ""id"": ""9286"", 
""nome"": ""Bairro Novo"", 
""num_lotes"": ""312"", 
""num_quadras"": ""9"", 
""plots_id"": ""159351"", 
""geo_data"": [ 
    { 
    ""loteamentos_id"": ""9286"", 
    ""G"": ""-7.27087569384820000000"", 
    ""K"": ""-34.90980863571200000000"", 
    ""index"": ""0"", 
    ""code"": ""V"" 
    }, 
    { 
    ""loteamentos_id"": ""9286"", 
    ""G"": ""-7.27234968660550000000"", 
    ""K"": ""-34.90971207618700000000"", 
    ""index"": ""1"", 
    ""code"": ""V"" 
    }, 
    { 
    ""loteamentos_id"": ""9286"", 
    ""G"": ""-7.27317448188540000000"", 
    ""K"": ""-34.90458905696900000000"", 
    ""index"": ""2"", 
    ""code"": ""V"" 
    }, 
    { 
    ""loteamentos_id"": ""9286"", 
    ""G"": ""-7.27176434710060000000"", 
    ""K"": ""-34.90472316741900000000"", 
    ""index"": ""3"", 
    ""code"": ""V"" 
    }, 
    { 
    ""loteamentos_id"": ""9286"", 
    ""G"": ""-7.27202508786680000000"", 
    ""K"": ""-34.90719884634000000000"", 
    ""index"": ""15"", 
    ""code"": ""C"" 
    } 
] 
    }"; 

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

public class GeoData 
{ 
    public string loteamentos_id { get; set; } 
    public string G { get; set; } 
    public string K { get; set; } 
    public string index { get; set; } 
    public string code { get; set; } 
} 

public class RootObject 
{ 
    public string id { get; set; } 
    public string nome { get; set; } 
    public string num_lotes { get; set; } 
    public string num_quadras { get; set; } 
    public string plots_id { get; set; } 
    public List<GeoData> geo_data { get; set; } 
} 

注:課程從樣品的Json創建了http://json2csharp.com

相關問題