Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Newtonsoft.Json反序列化JsonArray?_C#_Android_Arrays_Json_Deserialization - Fatal编程技术网

C# 如何使用Newtonsoft.Json反序列化JsonArray?

C# 如何使用Newtonsoft.Json反序列化JsonArray?,c#,android,arrays,json,deserialization,C#,Android,Arrays,Json,Deserialization,嗨,我有下面的代码,我想从中获得一些字段,并将其放入Android.TextView 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 Sy

嗨,我有下面的代码,我想从中获得一些字段,并将其放入Android.TextView

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




    }


}
我创建了一个包含以下字符串的类

 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; }
  }
类根对象
{
[JsonProperty(“订单”)]
公共结果{get;set;}
}
课堂成绩
{
[JsonProperty(“订单”)]
公共字典作业代码{get;set;}
}
类别工作代码
{
[JsonProperty(“orderId”)]
公共字符串orderId{get;set;}
[JsonProperty(“orderStatusCode”)]
公共字符串orderStatusCode{get;set;}
[JsonProperty(“orderStatusDescription”)]
公共字符串orderStatusDescription{get;set;}
}
我需要插入到应用程序中的字段是orderId orderStatusCode和orderStatusDescription

我正在尝试反序列化它,但它返回以下消息:参数1:无法从“CustomerJson”转换为“string”,并且无法使用未分配的局部变量“json”


我如何将其反序列化?

重要的事情

1) Order是orders[]数组的子元素

2) 客户是订单的子元素

您应该使用枚举器、循环等逐步在oders[]数组中搜索。。。然后,当每个循环完成时,您应该在order元素中解析Customer类

榜样

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

您的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
    }
  }]
}
此外,我还使用创建了模型,并将它们粘贴到下面。尝试用下面的模型解析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; }
}
公共类顺序2{
公共长医嘱ID{get;set;}
公共字符串orderStatusCode{get;set;}
公共字符串orderStatusDescription{get;set;}
public int serviceProviderId{get;set;}
公共字符串orderOpeningDate{get;set;}
公共字符串orderSchedulingDate{get;set;}
公共字符串orderSchedulingPeriod{get;set;}
公共对象orderSettlementDate{get;set;}
公共对象orderCancellationDate{get;set;}
}
公共阶级秩序{
公共秩序2秩序{get;set;}
}
公共类根对象{
公共列表顺序{get;set;}
}

当我尝试反序列化时,仍然出现无法转换的错误。var r=JsonConvert.DeserializeObject(r);我换了班,但还是有错误。请检查新类别。:)
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; }
}