2017-05-02 34 views
0

Hello guys我有一個應用程序,我試圖讓Json數組響應並將其放入TextView,但它有5個計數,但我不知道如何使用的閱讀這裏的JsonArray的每個字段是代碼:如何使用Xamarin Android閱讀JSON對象數組Newtonsoft.Json

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); 
      var requestToken = JsonConvert.DeserializeObject<RootObject>(answerorder.Content); 
      var parse = JObject.Parse(answerorder.Content); 
      var QtdeItens = parse.Count; 
      var end = ""; 
      /*foreach (Dictionary<string, Order2> kvp in Order) 
      { 
       txtorder.Text = "Id: " + kvp.Value.orderId; 

      }*/ 


     } 
     catch (Exception) 
     { 

      throw; 
     } 




    } 


} 

}

我創建了http://json2csharp.com/一類,這裏是我使用得到的值類。 對人一套,所以也可以每次我做這個測試時使用的ID 181.299.668-32

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Newtonsoft.Json; 

public class Order2 
{ 
public object 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; } 
    } 

所以答案是JSON的回報是diferent的ID號JSON答案depoends: {「orders」:[{「order」:{「orderId」:7004093603,「orderStatusCode」:「CANC」,「orderStatusDescription」:「Cancelado」,「serviceProviderId」:3649,「orderOpeningDate」:「2015-07 -07" , 「orderSchedulingDate」: 「2015年7月18日」, 「orderSchedulingPeriod」: 「M」, 「orderSettlementDate」:NULL, 「orderCancellationDate」:空}},{ 「順序」:{ 「訂單ID」:7004153791, 「orderStatusCode」: 「AGEN」, 「orderStatusDescription」: 「Agendado」, 「serviceProviderId」:3524, 「orderOpeningDate」: 「2016年8月31日」, 「orderSchedulingDate」: 「2016年9月1日」,「orderSchedulin gPeriod 「:」 M 「 」orderSettlementDate「:NULL, 」orderCancellationDate「:空}},{ 」順序「:{ 」訂單ID「:7004156972, 」orderStatusCode「: 」ABRT「, 」orderStatusDescription「: 」Aberto「,」 serviceProviderId 「:30820」,orderOpeningDate 「:」 2017年4月13" 日, 「orderSchedulingDate」:NULL, 「orderSchedulingPeriod」:NULL, 「orderSettlementDate」:NULL, 「orderCancellationDate」:空}},{ 「順序」:{」訂單ID 「:7002178478,」 orderStatusCode 「:」 CANC」, 「orderStatusDescription」: 「Cancelado」, 「serviceProviderId」:3555, 「orderOpeningDate」: 「2014年2月22日」, 「orderSchedulingDate」: 「2014年2月24日」 「orderSchedulingPeriod」: 「M」, 「orderSettlementDate」:NULL, 「orderCancellationDate」:空}},{ 「順序」:{ 「訂單ID」:7002118317, 「orderStatusCode」: 「CANC」, 「orderStatusDescription」: 「Cancelado」 「serviceProviderId」:3555, 「orderOpeningDate」: 「2014年2月10日」, 「orderSchedulingDate」: 「2014年2月15日」, 「orderSchedulingPeriod」: 「M」, 「orderSettlementDate」:空, 「orderCancellationDate」:空}}]}

而且JsonViewer說,它有5個計數,所以我該怎麼做了或的foreach讀取訂單ID,orderStatus代碼,orderStatusDescription和orderOpeningDate因爲dependiing上的ID爲從0至10計數

[編輯]

使用方法:

foreach (var order in requestToken.orders) 
      { 

        object vader = order.order.orderId; 
        string darth = Convert.ToString(vader); 
        txtorder.Text = darth; 
        txtorder.Text = order.order.orderStatusDescription; 
        txtorder.Text = order.order.orderStatusCode.; 
       } 

它返回一個訂單ID :)。我的疑問是:它自動調用計數0的orderId我該如何調用其他orderIds?是這樣嗎?

   foreach (var order in requestToken.orders) 
      { 
       //for (var i = 0; i < requestToken.orders.Count; i++) 
       //{ 
        object vader = order.order.orderId[1], [2], etc; 
        string darth = Convert.ToString(vader); 
        txtorder.Text = darth; 
        txtorder.Text = order.order.orderStatusDescription; 
        txtorder.Text = order.order.orderStatusCode.; 
       //} 

謝謝你們對我的幫助

+0

requestToken應該包含您所需要的所有信息。只要使用 的foreach(在requestToken.orders VAR順序){// 做你想做的 } –

回答

0

你需要反序列化JSON字符串

var data = JsonConvert.DeserializeObject<RootObject>(jsonString); 

然後運行foreach循環

foreach(var orders in data) 
{ 
    var Id = orders.order.orderId; 
    ///and so on... 
} 
+0

使用方法: foreach(var order in requestToken.orders) { object vader = order.order.orderId; string darth = Convert.ToString(vader); txtorder.Text = darth; txtorder.Text = order.order.orderStatusDescription; txtorder.Text = order.order.orderStatusCode .; } 它返回一個orderId :)。我的疑問是:它自動調用count 0的orderId,我怎樣才能調用其他orderIds? 謝謝 –

0

您可以訪問這樣的價值觀:

var test = JsonConvert.DeserializeObject<Rootobject>(json); 
var orderIds = new List<long>(); 
foreach (var order in test.orders) 
{ 
    var vader = order.order.orderId; 
    orderIds.Add(vader); 
} 

注意,orderIds被覆蓋到vader變量在每次循環。如果你想保留它們,你需要一個集合(在我的例子中名爲orderIds的列表)以及添加到該集合的每個迭代中。最後所有的orderId將在orderIds

+0

使用方法: 的foreach(在requestToken.orders VAR順序) { 對象維達= order.order.orderId ; string darth = Convert.ToString(vader); txtorder.Text = darth; txtorder.Text = order.order.orderStatusDescription; txtorder.Text = order.order.orderStatusCode .; } 它返回一個orderId :)。我的疑問是:它自動調用count 0的orderId,我怎樣才能調用其他orderIds? 謝謝 –

+0

你想提取所有的orderIds並將它們保存在某個地方,例如列表中? – Mahdi

+0

是的,我想提取他們,並在應用程序 –