您好我正在使用Xamarin來處理android。我編寫了一個代碼來從WSO2調用API,它返回的答案取決於用戶發送的ID。它返回的內容這樣的JSON:如何使用.Text序列化和反序列化Json var
{ 「consumerId」:56704292158, 「的categoryId」:1, 「categoryDescription」: 「佩索阿」, 「名字」: 「FN」,」 lastName「,」fiscalDocuments「:[{」fiscalDocument「:{」documentType「:」Brasil:nºCPF「,」cpf「:199999992}}],」gender「:」M「,」emailAddress「 : 「:R **** @ * COM」, 「optIns」:[{ 「選擇啓用」:{ 「名優產品」: 「領事」, 「acceptanceStatus」:假}},{ 「選擇啓用」:{ 「名優產品」: 「brastemp」, 「acceptanceStatus」:真}}], 「communicationData」:[{ 「communicationInfo」:{ 「addressId」:2 **** 03, 「PHONENUMBERS」:[{ 「phoneNumber的」:{ 「PHONETYPE」: 「mobilePhone」,「phoneNumber」:5555555555}}],「streetAddress」:「ANY STREET」,「streetNumber」:123,「streetComplement」:null,「streetDistrict」:「AN Y城市「,」cityName「:」ANY CITY「,」stateCode「:」XX「,」zipCode「:」12345「,」locationReferences「:[{」locationReference「:」Mercado「}],」countryCode「 ZZ 「}},{」 communicationInfo 「:{」 addressId 「:2.563244E + 7」,PHONENUMBERS 「:[{」 phoneNumber的 「:{」 PHONETYPE 「:」 mobilePhone 「 」phoneNumber的「:5555555555}},{」 phoneNumber的「:{」phoneType「:」homePhone「,」phoneNumber「:555555555}}],」streetAddress「:」ANY STREET「,」streetNumber「:123,」streetComplement「:」CASA「,」streetDistrict「 「,」cityName「:」ANY CITY「,」stateCode「:」XX「,」zipCode「:」12345「,」locationReferences「:[{」locationReference「:」Ao lado do deposito lua nova「}] 「:」ZZ「}}]}
我只想從這個字段獲取firstName和lastName。我試圖序列化這個,但我認爲我做錯了什麼。下面是代碼
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;
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 txtresposta;
RestClient orderId { get; set; }
RestRequest requestorderId { get; set; }
IRestResponse answer { get; set; }
IRestResponse answerorder { get; set; }
TextView txtnome;
public string raika;
private string teamname;
private JToken primeironome;
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);
txtresposta = FindViewById<TextView>(Resource.Id.txtresposta);
txtnome = FindViewById<TextView>(Resource.Id.txtNome);
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);
string nombre = Convert.ToString(mensagemConsumer.Content);
Nome nome = new Nome();
string json = JsonConvert.SerializeObject(mensagemConsumer.Content, Formatting.Indented);
nome.firstName = mensagemConsumer.Content;
Log.Info("info ",json);
txtresposta.Text = json;
//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);
txtnome.Text = answerorder.Content;
string respostaorder = Convert.ToString(answerorder);
dynamic deserializado = JsonConvert.DeserializeObject(respostaorder);
}
catch (Exception)
{
throw;
}
}
}
}
這裏是獲取類,並設置
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;
namespace App4.Resources
{
public class Nome
{
public string firstName { get; set;}
public string lastName { get; set; }
}
}
我如何序列化和與使用取決於像。文本一edtcpf.Text一個變種反序列化JSON這及其內容?
謝謝你這是有幫助的! –