JsonConvert.DeserializeObject returns some values in NULL

2

I'm working with a file in JSON that comes with a specific structure, which at the time of doing the deserialization process returns some of the values in 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 recover, regardless of whether it is only 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 make the conversion 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 within the List<FotoURL> list, which corresponds to the "v" section of the JSON file, which is precisely the most important part, since there are the images that I must recover.

    
asked by Juan Manuel Palacios 22.08.2018 в 19:10
source

1 answer

3

It is very important to bear in mind that the tools that JSON descerealizes, need the classes where they will leave their data. And to fill the values of those classes, 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, for reflection , look for the names of the properties of the class that match the JSON data.

In this case, know that your json has as header data (by putting a name) the elements t , v and st . Then you need the class to have those properties to know where to download that data.

However, your class is built like this:

public class ListaFotos
{
    public string t { get; set; }
    public List<FotoURL> fotos { get; set; }
    public string st { get; set; }
}

In which we see t and st , but we do not find v , instead we find the list photos. The descerealizador, does not know what to do with photos, and does not know where to send v (no, it's not smart!) And therefore, does not download the data from anywhere.

You must change the name in your class so that it matches with the names of the json like this:

public class ListaFotos
{
    public string t { get; set; }
    public List<FotoURL> v{ get; set; }
    public string st { get; set; }
}
    
answered by 22.08.2018 / 19:32
source