【问题标题】:c# - JSON with square brackets not possible to deserialize with RestSharpc# - 无法使用 RestSharp 反序列化带方括号的 JSON
【发布时间】:2016-10-18 09:16:49
【问题描述】:

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

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

【问题讨论】:

  • 简而言之,您的 JSON 代表一个数组,但您正在反序列化为单个对象。而是反序列化为List&lt;Class1&gt;
  • 谢谢,但return response.Data[0].entity.label; 仍然会抛出 NullReferenceException。还有其他想法吗?

标签: c# json deserialization restsharp


【解决方案1】:

您有两个选择,要么将您尝试序列化的对象更改为:

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,请保留序列化调用,就像现在一样。

【讨论】:

  • NullReferenceException: 对象引用未设置为对象的实例 PLZCounter.callGACWithPLZSandbox (System.String plz) (在 Assets/PLZCounter.cs:63) PLZCounter.Update () (在 Assets/PLZCounter. cs:47)
  • 好的,我无法更改 JSON。但是,如果我像这样更改对象,则返回 response.Data[0].entity.label;
  • 天啊,我讨厌这种“输入”行为...好吧,我无法更改 JSON。但是,如果我按照您告诉我的方式更改对象,则返回 response.Data[0].entity.label;我仍然得到一个 NullReferenceException。还有其他想法吗?
猜你喜欢
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 2023-02-23
相关资源
最近更新 更多