大家好,我有一个应用程序,我正在尝试获取Json数组响应并将其放入TextView,但它有5个计数,我不知道如何使用for来读取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);
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/创建了一个类,这是我用来获取值的类。 JSON答案取决于该人设置的身份证号码,因此每次使用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返回的答案是: { “订单”:[{ “顺序”:{ “订单ID”: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日”, “orderSchedulingPeriod” : “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月2日4" , “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”日期null}}]}
并且JsonViewer表示它有5个计数,所以我怎样才能执行for或foreach来读取orderId,orderStatus代码,orderStatusDescription和orderOpeningDate,因为它依赖于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.;
}
它返回了一个orderId :)。我的疑问是:它自动调用计数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 :(得分: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
。
答案 1 :(得分:0)
您需要反序列化JSON字符串
var data = JsonConvert.DeserializeObject<RootObject>(jsonString);
然后运行foreach循环
foreach(var orders in data)
{
var Id = orders.order.orderId;
///and so on...
}