Assez nouveau à C# et apprécierait toute aide. Obtention de l'erreur suivante: "La référence d'objet n'est pas définie sur une instance d'un objet." lorsque j'essaie d'accéder à une recette dans la liste de résultats. 0list. 0 ou tout autre élément de la liste.désérialisation de json retour null dans json correctement formaté
Cependant, je suis capable de renvoyer recipereturned.title avec succès.
Voici l'URL api au format JSON:
http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet&p=3
protected void getButton_Click(object sender, EventArgs e)
{
string url = string.Format("http://www.recipepuppy.com/api/?i={0}&q={1}", ingredientsTextBox.Text, recipetypeTextBox.Text);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
RecipeInfo recipeinforeturned = new RecipeInfo();
recipeinforeturned = (new JavaScriptSerializer()).Deserialize <RecipeInfo>(json);
//next need a loop to go through each list item and add to ResultsListBox to end of recipies
Label1.Text = recipeinforeturned.title;
for (int i = 0; i < recipeinforeturned.resultslist.Count; i++)
{
resultsListBox.Items.Add(recipeinforeturned.resultslist[i].title);
resultsListBox.Items.Add(recipeinforeturned.resultslist[i].ingredients);
resultsListBox.Items.Add(Environment.NewLine);
}
}
}
public class RecipeInfo
{
public string title { get; set; }
public string version { get; set; }
public string href { get; set; }
public List<Results> resultslist { get; set; }
}
public class Results
{
public string title { get; set; }
public string href { get; set; }
public string ingredients { get; set; }
public string thumbnail { get; set; }
}
}
} Toute aide serait grandement appréciée.
1. Utilisez http://jsonutils.com pour construire vos classes à partir de JSON (-Url) - 2. Utilisez JSON.Net pour la sérialisation JSON –