繁体   English   中英

带有方括号的JSON不能与RestSharp反序列化

[英]c# - JSON with square brackets not possible to deserialize with RestSharp

希望你能帮助我。 我想打印元素之一的“标签”。

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:对象引用未设置为对象的实例” ...

您有两个选择,可以将要序列化的对象更改为:

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,请立即保留序列化调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM