Reputation: 1146
I'm trying to deserialize an Object Array from an API service.
The data from the API has the following form:
{
"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"
}
]
},
The class 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; }
}
And the class GeoData:
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; }
}
The question is that I get an error and do not know how to get an Array.
In my code I have:
List<Loteamento>[] loteamentos = null;
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result);
What's wrong?
Upvotes: 1
Views: 1498
Reputation: 23837
Using Newtonsoft Json.Net you can deserialize it like this:
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; }
}
Note: Classes are created from sample Json on http://json2csharp.com
Upvotes: 0
Reputation: 1473
List<Loteamento>[] loteamentos = null;
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result);
Your first line declares loteamentos
as an array, where each cell in the array is a List<Loteamento>
. So this variable is set up to hold multiple instances of the type List<Loteamento>
.
Your second line then deserialises a single instance of List<Loteamento>
and then tries to assign this into the loteamentos
variable. The variable is unsuitable as it is an array of lists, not just a list.
I suspect it may work if you simply remove the []
from your first line.
Upvotes: 1