Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET C#JsonConvert.DeserializeObject数组_C#_.net - Fatal编程技术网

.NET C#JsonConvert.DeserializeObject数组

.NET C#JsonConvert.DeserializeObject数组,c#,.net,C#,.net,我正在尝试从API服务反序列化对象数组 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.9098086357120000

我正在尝试从API服务反序列化对象数组

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; }

}
问题是我遇到了一个错误,不知道如何获取数组

在我的代码中,我有:

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

List[]loteamentos=null;
List<Loteamento>[] loteamentos = null;
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result);
loteamentos=JsonConvert.DeserializeObject(dataObj.Result);
第一行将
loteamentos
声明为一个数组,其中数组中的每个单元格都是一个
列表
。因此,将此变量设置为保存类型
列表的多个实例

然后,第二行反序列化
List
的单个实例,然后尝试将其分配到
loteamentos
变量中。该变量不合适,因为它是一个列表数组,而不仅仅是一个列表


我怀疑如果您只需从第一行中删除
[]
,它可能会起作用。

使用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; }
}
void Main()
{
字符串json=@”{
“id”:“9286”,
“nome”:“Bairro Novo”,
“数量”:“312”,
“四头数”:“9”,
“绘图id”:“159351”,
“地理位置数据”:[
{
“loteamentos_id”:“9286”,
“G”:“7.27087569384820000000”,
“K”:“34.9098086357120000000”,
索引“”:“0”,
“代码”:“V”
},
{
“loteamentos_id”:“9286”,
“G”:“7.272349686605500000”,
“K”:“34.90971207618700000000”,
“索引”:“1”,
“代码”:“V”
},
{
“loteamentos_id”:“9286”,
“G”:“7.27317448188540000000”,
“K”:“34.9045890569690000000”,
“索引”:“2”,
“代码”:“V”
},
{
“loteamentos_id”:“9286”,
“G”:“7.27176434710060000000”,
“K”:“34.90472316741900000000”,
“索引”:“3”,
“代码”:“V”
},
{
“loteamentos_id”:“9286”,
“G”:“7.272025087668000000”,
“K”:“34.90719884634000000000”,
“索引”:“15”,
“代码”:“C”
}
]
}";
var result=JsonConvert.DeserializeObject(json);
}
公共类地理数据
{
公共字符串loteamentos_id{get;set;}
公共字符串G{get;set;}
公共字符串K{get;set;}
公共字符串索引{get;set;}
公共字符串代码{get;set;}
}
公共类根对象
{
公共字符串id{get;set;}
公共字符串nome{get;set;}
公共字符串num_lotes{get;set;}
公共字符串num_quadras{get;set;}
公共字符串绘图\u id{get;set;}
公共列表geo_数据{get;set;}
}

注意:类是根据样本Json在“我得到一个错误”上创建的。。。什么错误?还有,为什么你要把一个列表分配给一个列表数组?@patrickhoffman upss!你是对的。这就是问题所在!谢谢
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; }
}