2016-11-05 14 views
0

Xamarinを使用してAndroid用書き込みアプリです。リサイクルビュー(Xamarin Android)

リサイクラービューを作成しようとしています。ここで

は私のJSONです:http://pastebin.com/rL214a2T

私はこのようなクラスがあります。私はこのようなクラスを書く

public class Billing 
{ 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public string company { get; set; } 
    public string address_1 { get; set; } 
    public string address_2 { get; set; } 
    public string city { get; set; } 
    public string state { get; set; } 
    public string postcode { get; set; } 
    public string country { get; set; } 
    public string email { get; set; } 
    public string phone { get; set; } 
} 

public class Shipping 
{ 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public string company { get; set; } 
    public string address_1 { get; set; } 
    public string address_2 { get; set; } 
    public string city { get; set; } 
    public string state { get; set; } 
    public string postcode { get; set; } 
    public string country { get; set; } 
} 

public class Result 
{ 
    public int id { get; set; } 
    public int parent_id { get; set; } 
    public string status { get; set; } 
    public string order_key { get; set; } 
    public int number { get; set; } 
    public string currency { get; set; } 
    public string version { get; set; } 
    public bool prices_include_tax { get; set; } 
    public string date_created { get; set; } 
    public string date_modified { get; set; } 
    public int customer_id { get; set; } 
    public string discount_total { get; set; } 
    public string discount_tax { get; set; } 
    public string shipping_total { get; set; } 
    public string shipping_tax { get; set; } 
    public string cart_tax { get; set; } 
    public string total { get; set; } 
    public string total_tax { get; set; } 
    //public List<Billing> billing { get; set; } 
    public Billing billing { get; set; } 
    public Shipping shipping { get; set; } 
    public string payment_method { get; set; } 
    public string payment_method_title { get; set; } 
    public string transaction_id { get; set; } 
    public string customer_ip_address { get; set; } 
    public string customer_user_agent { get; set; } 
    public string created_via { get; set; } 
    public string customer_note { get; set; } 
    public string date_completed { get; set; } 
    public string date_paid { get; set; } 
    public string cart_hash { get; set; } 
    public List<object> line_items { get; set; } 
    public List<object> tax_lines { get; set; } 
    public List<object> shipping_lines { get; set; } 
    public List<object> fee_lines { get; set; } 
    public List<object> coupon_lines { get; set; } 
    public List<object> refunds { get; set; } 
} 

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

を:

using System; 
using System.Collections.Generic; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Threading.Tasks; 
using ModernHttpClient; 
using Newtonsoft.Json; 

namespace StarWars.Api.Repository 
{ 
    public class MoviesRepository 
    { 
     public async Task<RootObject> GetAllFilms() 
     { 
      var httpClient = GetHttpClient(); 

      var response = await httpClient.GetAsync(ServiceEndPoints.StartWarsApiBaseUri).ConfigureAwait(false); 

      if (response.IsSuccessStatusCode) 
      { 
       var content = response.Content; 

       string jsonString = await content.ReadAsStringAsync().ConfigureAwait(false); 


       return JsonConvert.DeserializeObject<RootObject>(jsonString); 
      } 
      return new RootObject(); 
     } 

     private HttpClient GetHttpClient() 
     { 
      var httpClient = new HttpClient(new NativeMessageHandler()) 
      { 
       BaseAddress = new Uri(ServiceEndPoints.StartWarsApiBaseUri) 
      }; 


      httpClient.DefaultRequestHeaders.Accept.Clear(); 

      httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

      return httpClient; 
     } 
    } 

    public class ServiceEndPoints 
    { 
     public static readonly string StartWarsApiBaseUri = "http://api.simplegames.com.ua/index.php/?wc_orders=processing_orders"; 
     // public static readonly string GetFilmsUri = "films"; 
    } 
} 

しかし、私はアプリを起動しようとすると、私はこれを持っていますエラー

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'StarWars.Api.Repository.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. 

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 

私が理解するように私は設定する必要があります

どうすれば修正できますか? ReSharperのを使用して

UPDATE

私はこの

public async Task<List<RootObject>> GetAllFilms() 
    { 
     var httpClient = GetHttpClient(); 

     var response = await httpClient.GetAsync(ServiceEndPoints.StartWarsApiBaseUri).ConfigureAwait(false); 

     if (response.IsSuccessStatusCode) 
     { 
      var content = response.Content; 

      string jsonString = await content.ReadAsStringAsync().ConfigureAwait(false); 


      return JsonConvert.DeserializeObject<List<RootObject>>(jsonString); 
     } 
     return new List<RootObject>(); 
    } 

    private HttpClient GetHttpClient() 
    { 
     var httpClient = new HttpClient(new NativeMessageHandler()) 
     { 
      BaseAddress = new Uri(ServiceEndPoints.StartWarsApiBaseUri) 
     }; 


     httpClient.DefaultRequestHeaders.Accept.Clear(); 

     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     return httpClient; 
    } 
} 

public class ServiceEndPoints 
{ 
    public static readonly string StartWarsApiBaseUri = "http://api.simplegames.com.ua/index.php/?wc_orders=processing_orders"; 
    // public static readonly string GetFilmsUri = "films"; 
} 

}

のようなコードをリメイクしかし、私はここでは、この行の私のMainActivityでエラーvar moviesAdapter = new MovieAdapter(films.results)

がエラー

である必要があり

エラーCS1061 'List'には 'results'の定義が含まれておらず、 'List'タイプの最初の引数を受け入れる拡張メソッド 'results'はありません(使用指示文またはアセンブリ参照がありません)

+0

jsonも共有できますか? –

+0

http://pastebin.com/rL214a2T @mww – Eugene

答えて

1

モデルクラスを変更する必要があります。モデルクラスの見た目がわからない場合はjson2sharpのようなツールを使用できます。クラスを使用してJSONを逆シリアル化することはできません。あなたの代わりにこのクラスを使用してください。

public class Billing 
{ 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public string company { get; set; } 
    public string address_1 { get; set; } 
    public string address_2 { get; set; } 
    public string city { get; set; } 
    public string state { get; set; } 
    public string postcode { get; set; } 
    public string country { get; set; } 
    public string email { get; set; } 
    public string phone { get; set; } 
} 

public class Shipping 
{ 
    public string first_name { get; set; } 
    public string last_name { get; set; } 
    public string company { get; set; } 
    public string address_1 { get; set; } 
    public string address_2 { get; set; } 
    public string city { get; set; } 
    public string state { get; set; } 
    public string postcode { get; set; } 
    public string country { get; set; } 
} 

public class LineItem 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string sku { get; set; } 
    public int product_id { get; set; } 
    public int variation_id { get; set; } 
    public int quantity { get; set; } 
    public string tax_class { get; set; } 
    public string price { get; set; } 
    public string subtotal { get; set; } 
    public string subtotal_tax { get; set; } 
    public string total { get; set; } 
    public string total_tax { get; set; } 
    public List<object> taxes { get; set; } 
    public List<object> meta { get; set; } 
} 

public class ShippingLine 
{ 
    public int id { get; set; } 
    public string method_title { get; set; } 
    public string method_id { get; set; } 
    public string total { get; set; } 
    public string total_tax { get; set; } 
    public List<object> taxes { get; set; } 
} 

public class RootObject 
{ 
    public int id { get; set; } 
    public int parent_id { get; set; } 
    public string status { get; set; } 
    public string order_key { get; set; } 
    public int number { get; set; } 
    public string currency { get; set; } 
    public string version { get; set; } 
    public bool prices_include_tax { get; set; } 
    public string date_created { get; set; } 
    public string date_modified { get; set; } 
    public int customer_id { get; set; } 
    public string discount_total { get; set; } 
    public string discount_tax { get; set; } 
    public string shipping_total { get; set; } 
    public string shipping_tax { get; set; } 
    public string cart_tax { get; set; } 
    public string total { get; set; } 
    public string total_tax { get; set; } 
    public Billing billing { get; set; } 
    public Shipping shipping { get; set; } 
    public string payment_method { get; set; } 
    public string payment_method_title { get; set; } 
    public string transaction_id { get; set; } 
    public string customer_ip_address { get; set; } 
    public string customer_user_agent { get; set; } 
    public string created_via { get; set; } 
    public string customer_note { get; set; } 
    public string date_completed { get; set; } 
    public string date_paid { get; set; } 
    public string cart_hash { get; set; } 
    public List<LineItem> line_items { get; set; } 
    public List<object> tax_lines { get; set; } 
    public List<ShippingLine> shipping_lines { get; set; } 
    public List<object> fee_lines { get; set; } 
    public List<object> coupon_lines { get; set; } 
    public List<object> refunds { get; set; } 
} 
+0

tax_lines、fee_lines、coupon_lines、refundsのデータはありませんが、可能なすべてのデータとhttp:// json2csharpを使用してjsonを作成する方が良いでしょう。 com/ –

+0

ありがとうございました。助けになる – Eugene

関連する問題