I’m getting back the following string from an API call:
{
"result":
[
{
"sys_id":"12d199028752a9108ae38516dabb35d3"
}
]
}
I’m trying to deserialize it into this class"
public class result
{
public string sys_id { get; set; }
}
I’m using this call: var x = Newtonsoft.Json.JsonConvert.DeserializeObject<result>(jsonString);
But "x.sys_id" is always coming up as NULL. What am I missing?
3
Answers
Your JSON models an object containing a
result
property containing an array with an object with asys_id
property, itself a string. However, yourresult
class is for that final object, the one withsys_id
. You’re therefore missing a level: you need something to contain those results.So let’s define a new class:
And then you can deserialize it:
The structure of the JSON response consists of an object which has a property
result
of typearray
, within which you have an object containing a single propertysys_id
of typestring
.As such, you would need the response to be mapped to something like the following:
You would then use it as:
sys_id is a string property of an object that is inside of an array, and so the object can be accessed by index only. But you don’t need any classes to obtain it.You can just parse a json string