How to read a JSON Object Array with Xamarin Android Newtonsoft.Json

Asked

Viewed 648 times

0

I am working on an app that gets a response JSON and inserts some fields of it into a textfield android, here is the code:

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




   }


}

I created a class on the site http://json2csharp.com/ and here it is, with the parameters to get the values, remembering that the value depends on the CPF I put, so it is not a fixed answer.

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

The answer that the JSON returns to Cpf 188.299.668.32 is:

{"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}}]}

I created a Count above to check how many vectors are, for this specific answer were 5 Counts, I managed to call 1 of them using the method:

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

Even though the orderId of Count 0, I need to know how to make him call the orderIds of the others Counts for me to put everything in the app. I tried this foreach down, but it didn’t work. How can I do that?

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

Thank you :)

1 answer

0


I don’t know if I understand what you want to do, but you can convert your entire string into the class that represents it:

var root = JsonConvert.DeserializeObject<RootObject>(stringRoot);

You will have access to the list members with a simple foreach in the result (in this case, root).

If that’s not what you want to do, please clarify the question so that I can help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.