2016-04-09 2 views
1

私はUWPで作業していますが、このJsonを解析しようとしていますが、なぜこのコードがうまく動作しないのかわかりません。ありがとうございました。リスト変数(リスト)を取得する際に問題があります。UWPでJsonを解析する

何か助けていただければ幸いです。

namespace testapi2 
{       


/// </summary> 
public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     jsonCall(); 
    } 
    public async void jsonCall() 
    { 
     List<Result> listResult = new List<Result>(); 
     var client = new HttpClient(); 
     HttpResponseMessage response = 
     await client.GetAsync(new Uri("http://api-public.guidebox.com/v1.43/Tunisia/rKgEWJbFg0kgEHrcGXPKhPDo0XtTafyC/movies/all/250/250")); 
     client.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); 
     var array = await response.Content.ReadAsStringAsync(); 
     JsonArray ja = JsonValue.Parse(array).GetArray();    
     for (uint i = 0; i < ja.Count; i++) 
     { 
      //Debug.WriteLine(i); 
      Result result = new Result(); 
      int id = Convert.ToInt32(ja.GetObjectAt(i).GetNamedValue("id")); 
      string title = ja.GetObjectAt(i).GetNamedString("title"); 
      int release_year = Convert.ToInt32(ja.GetObjectAt(i).GetNamedNumber("release_year")); 
      int themoviedb = Convert.ToInt32(ja.GetObjectAt(i).GetNamedString("themoviedb")); 
      string original_title = ja.GetObjectAt(i).GetNamedString("original_title"); 
      // List<object> alternate_titles = ja.GetObjectAt(i).GetNamedArray("alternate_titles"); 
      string imdb = ja.GetObjectAt(i).GetNamedString("imdb"); 
      bool pre_order = ja.GetObjectAt(i).GetNamedBoolean("pre_order"); 
      bool in_theaters = ja.GetObjectAt(i).GetNamedBoolean("in_theaters"); 
      string release_date = ja.GetObjectAt(i).GetNamedString("release_date"); 
      string rating = ja.GetObjectAt(i).GetNamedString("rating"); 
      int rottentomatoes = Convert.ToInt32(ja.GetObjectAt(i).GetNamedString("rottentomatoes")); 
      string freebase = ja.GetObjectAt(i).GetNamedString("freebase"); 
      int wikipedia_id = Convert.ToInt32(ja.GetObjectAt(i).GetNamedString("wikipedia_id")); 
      string metacritic = ja.GetObjectAt(i).GetNamedString("metacritic"); 
      string common_sense_media = ja.GetObjectAt(i).GetNamedString("common_sense_media"); 
      string poster_120x171 = ja.GetObjectAt(i).GetNamedString("poster_120x171"); 
      string poster_240x342 = ja.GetObjectAt(i).GetNamedString("poster_240x342"); 
      string poster_400x570 = ja.GetObjectAt(i).GetNamedString("poster_400x570"); 
      listResult.Add(result);       

     } 
     // Debug.WriteLine("hello", listResult); 
     list.ItemsSource = listResult; 

    } 
} 
} 

答えて

6

あなたのapiはjsonObject型の値を返し、それを配列に変換しようとします。

これを試すことができます。 最初にNewtonsoft.Jsonをadd referenceの管理用のnugetパッケージから追加します。

enter image description here

さて、このサイトjsonToC#にあなたのJSONレスポンスを貼り付け、プロジェクトにクラスに生成された追加。あなたの参照APIのために、このような2クラスを取得します。

public class Result 
{ 
    public int id { get; set; } 
    public string title { get; set; } 
    public int release_year { get; set; } 
    public int themoviedb { get; set; } 
    public string original_title { get; set; } 
    public List<object> alternate_titles { get; set; } 
    public string imdb { get; set; } 
    public bool pre_order { get; set; } 
    public bool in_theaters { get; set; } 
    public string release_date { get; set; } 
    public string rating { get; set; } 
    public int rottentomatoes { get; set; } 
    public string freebase { get; set; } 
    public int wikipedia_id { get; set; } 
    public string metacritic { get; set; } 
    public string common_sense_media { get; set; } 
    public string poster_120x171 { get; set; } 
    public string poster_240x342 { get; set; } 
    public string poster_400x570 { get; set; } 
} 

public class RootObject 
{ 
    public int total_results { get; set; } 
    public int total_returned { get; set; } 
    public List<Result> results { get; set; } 
} 

は、結果にすべてのデータを取得するコード&次の行を追加します。

var json = await response.Content.ReadAsStringAsync(); 
var result= JsonConvert.DeserializeObject<RootObject>(json); 
+0

ありがとうございました。 これを修正する方法を教えてください。 //リスト alternate_titles = ja.GetObjectAt(i).GetNamedArray( "alternate_titles"); –

+0

お友達ありがとうございました、幸せなユース最後のものリスト alternate_titlesどのように書かれますか?ありがとうございます –

+0

こんにちはWassim、今すぐご確認ください。遅れてごめん。 –