2017-09-23 2 views
0

C#の初心者であり、助けていただければ幸いです。jsonの非直列化は、適切にフォーマットされたjsonでnullを返します。

次のエラーの取得: "オブジェクト参照がオブジェクトのインスタンスに設定されていません。"私がアクセスしようとすると、recipeinforeturned.resultslist [0] .titleまたはリスト内の他の要素。

しかし、私はrecipereturned.titleを正常に返すことができます。

はここでJSON形式でのAPIのURLです:

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; } 
    } 
} 

} 任意の助けいただければ幸いです。

+0

を返します。 1. http://jsonutils.comを使用してJSON(-Url) - 2からクラスを構築します。JSON.Net JSONのシリアル化 –

答えて

0

public List<Results> results { get; set; } 

public List<Results> resultslist { get; set; } 

をそうしないとデシリアライザは、あなたのJSONにresultslistオブジェクトを探しているが、それを見つけることができませんのでnull

0

"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; } 
} 
+0

のために、実際にあなたは私の問題を解決しました。クラスのリストを間違った名前で呼びました。私はちょうどあなたの応答を見てそれをキャッチしました。どうもありがとうございます!! – JBiz

関連する問題