json return null 的反序列化格式正确 json
deserialization of json return null in properly formatted json
刚接触 C#,希望得到任何帮助。
出现以下错误:
"Object reference not set to an instance of an object." 当我尝试访问 recipeinforeturned.resultslist[0].title 或列表中的任何其他元素时。
不过,我能够 return recipereturned.title 成功。
这里是 api URL 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; }
}
}
}
任何帮助将不胜感激。
您可以尝试将"Version"字符串的字段类型改为double。
public class RootObject
{
public string title { get; set; }
public double version { get; set; }
public string href { get; set; }
public List<Result> results { get; set; }
}
您需要重命名:
public List<Results> resultslist { get; set; }
到
public List<Results> results { get; set; }
否则反序列化器正在您的 JSON 中寻找 resultslist
对象但找不到它,因此 returns null
刚接触 C#,希望得到任何帮助。
出现以下错误: "Object reference not set to an instance of an object." 当我尝试访问 recipeinforeturned.resultslist[0].title 或列表中的任何其他元素时。
不过,我能够 return recipereturned.title 成功。
这里是 api URL 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; }
}
}
} 任何帮助将不胜感激。
您可以尝试将"Version"字符串的字段类型改为double。
public class RootObject
{
public string title { get; set; }
public double version { get; set; }
public string href { get; set; }
public List<Result> results { get; set; }
}
您需要重命名:
public List<Results> resultslist { get; set; }
到
public List<Results> results { get; set; }
否则反序列化器正在您的 JSON 中寻找 resultslist
对象但找不到它,因此 returns null