[英]c# - JSON with square brackets not possible to deserialize with RestSharp
hopefully you can help me. 希望你能帮助我。 I want to print the "label" of one of the elements. 我想打印元素之一的“标签”。
JSON input: 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
}
]
}
]
And the API call with JsonDeserializer 然后使用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;
}
And, the classes 而且,课程
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; }
}
What am I doing wrong? 我究竟做错了什么? Result is always "NullReferenceException: Object reference not set to an instance of an object"... 结果始终为“ NullReferenceException:对象引用未设置为对象的实例” ...
You have two options either change the object you're trying to serialize to: 您有两个选择,可以将要序列化的对象更改为:
IRestResponse<List<Class1>> response = client.Execute<List<Class1>>(request);
Or modify your Json to 或将您的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
}
]
}
]
}
If you modify your json leave the serializing call as you have it now. 如果您修改json,请立即保留序列化调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.