I have the below response from the API in a console application written in C#:
{
"d": {
"results": [
{
"__metadata": {
"id": "123",
"uri": "456",
"type": "789"
},
"PERNR": "1",
"USRID": "2"
},
{
"__metadata": {
"id": "159",
"uri": "951",
"type": "753"
},
"PERNR": "4",
"USRID": "6"
}
]
}
}
And used the below code to deserialize:
public class d
{
public results results { get; set; }
}
public class results
{
public string PERNR { get; set; }
public string USRID { get; set; }
public __metadata __metadata { get; set; }
}
public class __metadata
{
public string id { get; set;}
public string uri { get; set; }
public string type { get; set; }
}
var serilizer = new JavaScriptSerializer();
d output = serilizer.Deserialize<d>(response.Content);
But the result is null. Is there any problem in the definition of the classes?
3
Answers
The issue is results is an array in your json, where in your class, it is an object. Change it to
The properties should start with a capital letter, to match the case of the properties in the JSON response. Change the class definitions as follows:
Change deserialize line to:
If you use a JSON to C# converter like json2csharp or Visual Studio’s Paste JSON As Classes you’ll get :
There are two important differences :
d
, not aD
objectD.results
is a collection ofResult
objects, not a single object