Reputation: 585
I have this code in a Xamarin type solution
public async Task<Response> GetList<Publication>(string urlBase, string servicePrefix, string controller)
{
try
{
var client = new HttpClient();
client.BaseAddress = new Uri(urlBase);
var url = string.Format("{0}{1}", servicePrefix, controller);
var response = await client.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
return new Response
{
IsSuccess = false,
Message = result,
};
}
var list = JsonConvert.DeserializeObject<List<Publication>>(result);
return new Response
{
IsSuccess = true,
Message = "Ok",
Result = list,
};
}
catch (Exception ex)
{
return new Response
{
IsSuccess = false,
Message = ex.Message,
};
}
}
when I debug it, JsonConvert.DeserializeObject<List<Publication>>(result);
doesn't work, return null...
DEBUG IN XAMARIN
I'm sure result is not null
But if I run the same code in a console type solution, It Works!!!
I'm using the same class, there is my Publication class
public class Publication
{
public int IdPublicacion { get; set; }
public string UrlVideo { get; set; }
public object UrlImagen { get; set; }
public string Text { get; set; }
public string Titulo { get; set; }
public DateTime Fecha { get; set; }
public string Sacerdote { get; set; }
}
And there is my Json Code
[
{
"IdPublicacion": 1,
"UrlVideo": "https://www.youtube.com/watch?v=mQR0bXO_yI8",
"UrlImagen": null,
"Text": "Para fomentar la lectura en los niños es recomendable empezar con cuentos infantiles cortos que traten de aventuras divertidas y que capten la atención de los niños. De esta forma, los niños se divertirán a la vez que empiezan a cogerle el gusto a la lectura.\r\n \r\nLos relatos cortos con son los mejores para empezar a leer con los niños. Aunque hay multitud de cuentos tradicionales que son esenciales y que todo niño debería conocer, ya que han ido pasando de generación en generación.\r\n\r\nEn pequelandia.org se han seleccionado una serie de cuentos infantiles cortos para leer con niños. Son relatos cortos para hacer de la lectura un momento agradable y divertido, de forma que los niños empiecen a familiarizarse con la lectura y los libros.",
"Titulo": "Fly me to the moon",
"Fecha": "2018-03-07T00:00:00",
"Sacerdote": "Julian"
},
{
"IdPublicacion": 2,
"UrlVideo": "https://www.youtube.com/watch?v=mQR0bXO_yI8",
"UrlImagen": null,
"Text": "Para fomentar la lectura en los niños es recomendable empezar con cuentos infantiles cortos que traten de aventuras divertidas y que capten la atención de los niños. De esta forma, los niños se divertirán a la vez que empiezan a cogerle el gusto a la lectura.\r\n \r\nLos relatos cortos con son los mejores para empezar a leer con los niños. Aunque hay multitud de cuentos tradicionales que son esenciales y que todo niño debería conocer, ya que han ido pasando de generación en generación.",
"Titulo": "Titulo 2",
"Fecha": "2018-03-06T00:00:00",
"Sacerdote": "Julian"
}]
Additional information, when I see the List, show a message
"Unable to cast object of type 'System.RuntimeType' to type 'Mono.Debugger.Soft.TypeMirror'."
Finally there is my GitHub project https://github.com/solartes/VesApp
Upvotes: 0
Views: 609
Reputation: 585
SOLVED.
THE PROBLEM? JSON.NET has problem with the Xamarin Live Player, so it doesn't works if you play it in Xamarin Live Player, you have to run it in Android Simulator.
Upvotes: 2
Reputation: 51
I realized that in your Debug you did not specify the type that you want to deserialize your json to List< T >. However, in your test that worked, you did specify that you need a list of Publications back "List< Publication >" .
Upvotes: 0
Reputation: 39082
Try to add Preserve
attribute to the Publication
class:
[Preserve(AllMembers = true)]
public class Publication
It might happen that the build strips out the properties of the class and hence they are missing and cannot be deserialized.
Upvotes: 0