[英]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.