I have this JSON file:
{
"result":
[
{
"desc" : "Ok",
"cod" : "1"
}
],
"data":
[
{
"cod" : "95B86DF6AE282E67B6B7437D09570847"
}
]
}
A method which deserializes it
protected void Deserialize()
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"\token.json";
string file = System.IO.File.ReadAllText(path);
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<data>(file);
}
public class result
{
public int cod { get; set; }
public string desc{ get; set; }
}
public class data
{
public string cod{ get; set; }
}
The problem is that it doesn't deserialize it, and creates empty Data object. I'm missing something but I don't know what, hope someone will help me.
Your data model does not correspond to the JSON object, and the serializer is not able to deserialize it properly. Notice that inside that object you have arrays of objects, so the correct structure you need to deserialize that would be something like:
public class Token
{
public Result[] result { get; set; }
public Data[] data { get; set; }
}
And then you can do:
var res = JsonConvert.DeserializeObject<Token>(file);