I am working with a JSON file that comes with a specific structure, which when doing the deserialization process returns some of the values to null.
The source file that I am processing is the following:
{
"t":"i",
"v":
[
{
"id":604618,
"url":"http://wsplus.navego360.com/images/services/215/37480914/604618.jpg",
"last_log":
{
"t":"n",
"dt":"2018-08-16T19:03:47.962Z",
"c_lat":-12.014695,
"c_lon":-76.93001
}
},
{
"id":604619,
"url":"http://wsplus.navego360.com/images/services/215/37480914/604619.jpg",
"last_log":
{
"t":"n",
"dt":"2018-08-16T19:03:55.667Z",
"c_lat":-12.014695,
"c_lon":-76.93001
}
}
],
"st":"p"
}
This file can return several URLs with different photos, which I must be able to retrieve, regardless of whether it is just one URL or several.
The structure in C# that will receive the values is as follows:
public class ListaFotos
{
public string t { get; set; }
public List<FotoURL> fotos { get; set; }
public string st { get; set; }
}
public class FotoURL
{
public string id { get; set; }
public string url { get; set; }
public IDictionary<string, string> last_log { get; set; }
}
To convert to C# I am using the following line of code:
JsonConvert.DeserializeObject<DatosRetorno.ListaFotos>(jsonFotos);
The values that I am not receiving are those that are inside the list List<FotoURL>
, which corresponds to the "v" section of the JSON file, which is precisely the most important part, since the images that I must retrieve are located there.
It is very important to keep in mind that the tools that decerealize JSON need the classes where they are going to leave the JSON data. And to populate the values of those classes, they use the values that appear in the JSON.
JsonConvert.DeserializeObject
uses the class you passed to it, to transform the JSON data to that class. And for that, byreflection
, it looks for the names of the properties of the class that match the data in the JSON.In this case, know that your json has as data at the header level (to put a name) the elements
t
,v
andst
. So you need the class to have those properties to know where to download that data.However, your class is built like this:
In which we see
t
andst
, but we don't findv
, instead we find the photos list. The decerealizer doesn't know what to do with photos, and doesn't know where to send v (no, it's not smart!) and therefore doesn't download the data from anywhere.You must change the name in your class to match the json names like so: