2017-04-26 15 views
0

こんにちは、私は次のコードを持って、それから、私はいくつかのフィールドを取得し、Android.TextViewにNewtonsoft.Jsonを使用してJsonArrayを逆シリアル化する方法

using System; 
using Android.App; 
using Android.Widget; 
using Android.OS; 
using RestSharp; 
using Newtonsoft.Json; 
using Android.Util; 
using App4.Resources; 
using Newtonsoft.Json.Linq; 
using Org.Json; 
using System.Net; 
using System.IO; 
using System.Collections.Generic; 

namespace App4 
{ 
[Activity(Label = "App4", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    EditText edtcpf; 
    Button btnConsumer; 
    TextView txtcpf; 
    RestRequest cpf { get; set; } 
    public RestClient consumer { get; set; } 
    IRestResponse mensagemConsumer; 
    TextView txtsobrenome; 
    RestClient orderId { get; set; } 
    RestRequest requestorderId { get; set; } 
    IRestResponse answerorder { get; set; } 
    TextView txtnome; 
    TextView txtorder; 
    TextView txtmensagem; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 
     btnConsumer = FindViewById<Button>(Resource.Id.btnConsumer); 
     edtcpf = FindViewById<EditText>(Resource.Id.edtcpf); 
     txtcpf = FindViewById<TextView>(Resource.Id.txtcpf); 
     txtsobrenome = FindViewById<TextView>(Resource.Id.txtresposta); 
     txtnome = FindViewById<TextView>(Resource.Id.txtNome); 
     txtorder = FindViewById<TextView>(Resource.Id.txtorder); 
     txtmensagem = FindViewById<TextView>(Resource.Id.txtMensagem); 
     btnConsumer.Click += BtnConsumer_Click; 

    } 

    private void BtnConsumer_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      // API Consumer CPF 

      consumer = new RestClient("https://qa.api-latam.whirlpool.com/v1.0/consumers"); 
      cpf = new RestRequest("/" + edtcpf.Text, Method.GET); 
      cpf.AddHeader("Content-Type", "application/json; charset=utf-8"); 
      cpf.AddHeader("Authorization", "Bearer 70197e6c-d81b-384c-bb32-d69e8c10b101"); 
      mensagemConsumer = consumer.Execute(cpf); 
      Pessoa pessoa = JsonConvert.DeserializeObject<Pessoa>(mensagemConsumer.Content); 
      txtnome.Text = "Nome: " +pessoa.firstName; 
      txtsobrenome.Text = "Sobrenome: "+ pessoa.lastName; 

      // API Consumer Appliances 
      orderId = new RestClient("https://qa.api-latam.whirlpool.com/v1.0/consumers/"); 
      requestorderId = new RestRequest("/"+ edtcpf.Text+ "/service-orders", Method.GET); 
      requestorderId.AddHeader("Content-Type", "application/json; charset=utf-8"); 
      requestorderId.AddHeader("Authorization", "Bearer 70197e6c-d81b-384c-bb32-d69e8c10b101"); 
      answerorder = orderId.Execute(requestorderId); 
      CustomerJson json = JsonConvert.DeserializeObject<List<CustomerJson>>(json); 
      txtorder.Text = json.Customer.orderId; 





     } 
     catch (Exception) 
     { 

      throw; 
     } 




    } 


} 

}

そして私が配列であるJsonResponseをデシリアライズしようとしているを入れたいですここで私はAPIから取得応答です:私はこの文字列を持つクラスを作成し

**{ 
    "orders": [ 
    { 
    "order": { 
    "orderId": 7004093603, 
    "orderStatusCode": "CANC", 
    "orderStatusDescription": "Cancelado", 
    "serviceProviderId": 3649, 
    "orderOpeningDate": "2015-07-07", 
    "orderSchedulingDate": "2015-07-18", 
    "orderSchedulingPeriod": "M", 
    "orderSettlementDate": null, 
    "orderCancellationDate": null 
    } 
}, 
{ 
    "order": { 
    "orderId": 7004153791, 
    "orderStatusCode": "AGEN", 
    "orderStatusDescription": "Agendado", 
    "serviceProviderId": 3524, 
    "orderOpeningDate": "2016-08-31", 
    "orderSchedulingDate": "2016-09-01", 
    "orderSchedulingPeriod": "M", 
    "orderSettlementDate": null, 
    "orderCancellationDate": null 
    } 
}, 
{ 
    "order": { 
    "orderId": 7004156972, 
    "orderStatusCode": "ABRT", 
    "orderStatusDescription": "Aberto", 
    "serviceProviderId": 30820, 
    "orderOpeningDate": "2017-04-13", 
    "orderSchedulingDate": null, 
    "orderSchedulingPeriod": null, 
    "orderSettlementDate": null, 
    "orderCancellationDate": null 
    } 
}, 
{ 
    "order": { 
    "orderId": 7002178478, 
    "orderStatusCode": "CANC", 
    "orderStatusDescription": "Cancelado", 
    "serviceProviderId": 3555, 
    "orderOpeningDate": "2014-02-22", 
    "orderSchedulingDate": "2014-02-24", 
    "orderSchedulingPeriod": "M", 
    "orderSettlementDate": null, 
    "orderCancellationDate": null 
    } 
}, 
{ 
    "order": { 
    "orderId": 7002118317, 
    "orderStatusCode": "CANC", 
    "orderStatusDescription": "Cancelado", 
    "serviceProviderId": 3555, 
    "orderOpeningDate": "2014-02-10", 
    "orderSchedulingDate": "2014-02-15", 
    "orderSchedulingPeriod": "M", 
    "orderSettlementDate": null, 
    "orderCancellationDate": null 
    } 
    } 
    ] 
    }** 

class RootObject 
{ 
[JsonProperty("orders")] 
public Results Results { get; set; } 
} 

class Results 
{ 
[JsonProperty("order")] 
public Dictionary<string, JobCode> JobCodes { get; set; } 
} 

class JobCode 
{ 
[JsonProperty("orderId")] 
public string orderId { get; set; } 
[JsonProperty("orderStatusCode")] 
public string orderStatusCode { get; set; } 
[JsonProperty("orderStatusDescription")] 
public string orderStatusDescription { get; set; } 
    } 

アプリケーションに挿入する必要があるフィールドは、orderId orderStatusCodeとorderStatusDescriptionです。

私はそれをデシリアライズしようとしているが、それは、このメッセージが返されます。引数1:未割り当てのローカル変数「JSON」の文字列 'と使用するために「CustomerJson」から変換することはできません

は、どのように私はこれをデシリアライズすることができます?

答えて

0

重要なこと

1)注文は、注文[]配列のサブ要素です。

2)CustomerはOrderの下位要素です。

エニュアレータ、ループなどでoders []配列を段階的に検索する必要があります。そして、すべてのループが完了すると、order要素のCustomerクラスを解析する必要があります。

例;

foreach(var order in orders) 
{ 
    var order = order.name; 
    //access to order data; 
} 

Deserialize JSON to C# Classes

+0

クラスを変更しましたが、まだエラーが発生しています。新しいクラスをチェックしてください。 :) –

0

あなたのJSONが間違っている、あなたは '}' 逃しています。ここで有効なJSON

{ 
    "orders": [{ 
    "order": { 
     "orderId": 7004093603, 
     "orderStatusCode": "CANC", 
     "orderStatusDescription": "Cancelado", 
     "serviceProviderId": 3649, 
     "orderOpeningDate": "2015-07-07", 
     "orderSchedulingDate": "2015-07-18", 
     "orderSchedulingPeriod": "M", 
     "orderSettlementDate": null, 
     "orderCancellationDate": null 
    } 
    }] 
} 

はまた、私は以下にそれらを貼り付けていhttp://json2csharp.com/を使用してモデルを作成しています。次のモデルでjsonを解析してみてください。

public class Order2 { 
    public long orderId { get; set; } 
    public string orderStatusCode { get; set; } 
    public string orderStatusDescription { get; set; } 
    public int serviceProviderId { get; set; } 
    public string orderOpeningDate { get; set; } 
    public string orderSchedulingDate { get; set; } 
    public string orderSchedulingPeriod { get; set; } 
    public object orderSettlementDate { get; set; } 
    public object orderCancellationDate { get; set; } 
} 

public class Order{ 
    public Order2 order { get; set; } 
} 

public class RootObject{ 
    public List<Order> orders { get; set; } 
} 
+0

デシリアライズしようとすると、まだ変換できないというエラーが表示されます。 var r = JsonConvert.DeserializeObject (r); –

関連する問題