Reputation: 221
I need an example how of to build a class for a JSON response with JSON.NET.
I am calling the following, that requires a class ResponseCall
that I have to build:
JsonConvert.DeserializeObject<ResponseCall>(result);
My problem is that I am not sure what exactly are all the nested attributes in a class. What is "responseHeader" and the nested attributes "Status" and how do I access them?
Here is the JSON:
{
"responseHeader": {
"status": 0,
"QTime": 1801,
"params": {
"fl": "id_tlc,ccZzcucbez_tlc,ekonr_tlc,gtin_tlc,region_tlc",
"sort": "ccZzcucbez_tlc asc",
"indent": "on",
"start": "0",
"q": "indexName:b2cLMIVProdukteIndex AND ( (id_tlc:*000006757 OR gtin_tlc:6757 OR (addGtin_tlc:6757,* OR addGtin_tlc:*,6757,* OR addGtin_tlc:*,6757 ) OR ekonr_tlc:6757))",
"wt": "json",
"qt": "",
"hl": "true",
"fq": "",
"version": "2.2",
"rows": "10"
}
},
"response": {
"numFound": 1,
"start": 0,
"docs": [
{
"ekonr_tlc": "1030860",
"region_tlc": "NBST",
"ccZzcucbez_tlc": "GT EHG ERDINGER WEISSB.DKL.20X0,5L",
"id_tlc": "NBST_000000000135459003",
"gtin_tlc": "4002103010036"
}
]
},
"highlighting": {
"b2cLMIVProdukteIndex_NBST_000000000135459003": {
}
}
}
Upvotes: 0
Views: 84
Reputation: 129697
Try using this class structure:
class ResponseCall
{
[JsonProperty("responseHeader")]
public ResponseHeader ResponseHeader { get; set; }
[JsonProperty("response")]
public Response Response { get; set; }
[JsonProperty("highlighting")]
public Dictionary<string, object> Highlighting { get; set; }
}
class ResponseHeader
{
[JsonProperty("status")]
public int Status { get; set; }
[JsonProperty("QTime")]
public int QTime { get; set; }
[JsonProperty("params")]
public Dictionary<string, string> Params { get; set; }
}
class Response
{
[JsonProperty("numFound")]
public int NumFound { get; set; }
[JsonProperty("start")]
public int Start { get; set; }
[JsonProperty("docs")]
public List<Dictionary<string, string>> Docs { get; set; }
}
Then deserialize like this:
ResponseCall rc = JsonConvert.DeserializeObject<ResponseCall>(json);
Fiddle: https://dotnetfiddle.net/FdQu7U
Upvotes: 4