我正在尝试从API服务反序列化对象数组。
API中的数据具有以下形式:
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private OnEntryClickListener mOnEntryClickListener;
public interface OnEntryClickListener {
void onEntryClick(View view, int position);
}
public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
mOnEntryClickListener = onEntryClickListener;
}
}
Loteamento课程:
{
"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"
}
]
},
类GeoData:
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; }
}
怎么了?
答案 0 :(得分:1)
List<Loteamento>[] loteamentos = null;
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result);
您的第一行将loteamentos
声明为数组,其中数组中的每个单元格都是List<Loteamento>
。因此,此变量设置为保存类型为List<Loteamento>
的多个实例。
然后您的第二行反序列化单个 List<Loteamento>
实例,然后尝试将其分配到loteamentos
变量中。该变量不适合,因为它是一个列表数组,而不仅仅是一个列表。
如果您只是从第一行删除[]
,我怀疑它可能有用。
答案 1 :(得分: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; }
}
注意:类是从http://json2csharp.com
上的示例Json创建的