使用RestSharp无法反序列化带有方括号的JSON

本文关键字:方括号 JSON 反序列化 RestSharp 使用 | 更新日期: 2023-09-27 18:13:23

希望你能帮助我。我想打印其中一个元素的"label"

JSON输入:

[
  {
    "entity":{
      "type":"postcode",
      "id":"P11516",
      "label":"18314 Divitz-Spoldershagen",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  },
  {
    "entity":{
      "type":"postcode",
      "id":"P11541",
      "label":"18314 Kenz-Küstrow",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  },
  {
    "entity":{
      "type":"postcode",
      "id":"P11549",
      "label":"18314 Löbnitz",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  },
  {
    "entity":{
      "type":"postcode",
      "id":"P11551",
      "label":"18314 Lüdershagen",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  }
]

和JsonDeserializer的API调用

public string callGACWithPLZSandbox(string plz)
{
    var client = new RestClient("http://rest.sandbox-immobilienscout24.de");
    var request = new RestRequest("restapi/api/gis/v2.0/geoautocomplete/DEU", Method.GET);
    client.ClearHandlers();
    client.AddHandler("application/json", new JsonDeserializer());
    request.AddQueryParameter("i", plz);
    request.AddQueryParameter("t", "postcode");
    request.AddHeader("bla", "blub");
    IRestResponse<Rootobject> response = client.Execute<Rootobject>(request);
    return response.Data.Property1[1].entity.label;
}

class Rootobject
{
    public Class1[] Property1 { get; set; }
}
class Class1
{
    public Entity entity { get; set; }
    public Match[] matches { get; set; }
}
class Entity
{
    public string type { get; set; }
    public string id { get; set; }
    public string label { get; set; }
    public string value { get; set; }
}
 class Match
{
    public int offset { get; set; }
    public int length { get; set; }
}

我做错了什么?结果总是"NullReferenceException: Object reference not set to a instance of Object "…

使用RestSharp无法反序列化带有方括号的JSON

您有两种选择:要么更改您试图序列化的对象:

IRestResponse<List<Class1>> response = client.Execute<List<Class1>>(request);

或者将Json修改为

{
 "Property1" : [
  {
    "entity":{
      "type":"postcode",
      "id":"P11516",
      "label":"18314 Divitz-Spoldershagen",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  },
  {
    "entity":{
      "type":"postcode",
      "id":"P11541",
      "label":"18314 Kenz-Küstrow",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  },
  {
    "entity":{
      "type":"postcode",
      "id":"P11549",
      "label":"18314 Löbnitz",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  },
  {
    "entity":{
      "type":"postcode",
      "id":"P11551",
      "label":"18314 Lüdershagen",
      "value":"18314"
    },
    "matches":[
      {
        "offset":0,
        "length":5
      }
    ]
  }
]
}

如果你修改了你的json,让序列化调用保持现在的状态