如何遍历列表字典并打印其内容?

How to iterate over a list dictionaries and print their content?

我是一个 c# 新手,我想出了使用 HttpClient 从 API 得到响应,并使用以下代码我得到了 JSON来自如下所示的服务器:

class Program
{
    public class Parameter
    {
        public Usuario Usuario { get; set; }
        public Establecimiento Establecimiento { get; set; }
    }
    public class Usuario
    {
        public string email { get; set; }
        public string password { get; set; }
    }

    public class Establecimiento
    {
        public int id { get; set; }
    }

    public class deliveryData
    {
        public string id { get; set; }
        public string establecimiento_id { get; set; }
        public string numero_orden { get; set; }
        public string ciudad_id { get; set; }
        public string fecha { get; set; }
    }

    static void Main()
    {
        try
        {
            RunAsync().Wait();
            Console.ReadLine();
        }
        catch (AggregateException e)
        {
            Console.WriteLine("Exception details: " + e.ToString());
            Console.ReadLine();
        }
    }

    static async Task RunAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://it-215...web.development.co/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            try
            {
                // HTTP POST
                var json = new Parameter
                {
                    Establecimiento = new Establecimiento
                    {
                        id = 147
                    },
                    Usuario = new Usuario
                    {
                        email = "user@email.com",
                        password = "something"
                    }
                };

                HttpResponseMessage response = await client.PostAsJsonAsync("api/service", json);
                response.EnsureSuccessStatusCode();    // Throw if not a success code.
                string responseBodyAsText;
                responseBodyAsText = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBodyAsText);

                //List<Dictionary<string, deliveryData>> decodedDeliveries = JsonConvert.DeserializeObject<List<Dictionary<string, deliveryData>>>(responseBodyAsText);

                //foreach (var delivery in decodedDeliveries)
                //{
                //    Console.WriteLine(delivery.ToString());
                //}

            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("Exception details: " + e.ToString());
            }
        }
    }
}

JSON 回复到这里:

[   {
    "PedidosOnline": {
      "id": "7491031",
      "establecimiento_id": "147",
      "numero_orden": "1769629-20160509211442",
      "fecha": "2016-05-09 21:14:42"
    }   
    },   {
    "PedidosOnline": {
      "id": "7491328",
      "establecimiento_id": "147",
      "numero_orden": "1559397-20160509212644",
      "fecha": "2016-05-09 21:26:44"
    }   
} ]

现在当我评论

Console.WriteLine(responseBodyAsText);

并取消注释解码行和 foreach,这就是我得到的:

System.Collections.Generic.Dictionary`2[System.String,ProjectName.Program+deliveryData]
System.Collections.Generic.Dictionary`2[System.String,ProjectName.Program+deliveryData]

我想要的是获得在 JSON 中查看的字段的清晰打印,因为我的下一步是将字典的每个字段保存在 Access 数据库中(我不知道如何去做,但我会弄清楚的)。所以我需要一点帮助,任何建议都将不胜感激。

非常感谢。

不是打印 Dictionary 对象本身,而是遍历每个 Dictionary 并打印每个键值对。

List<Dictionary<string, deliveryData>> decodedDeliveries = JsonConvert.DeserializeObject<List<Dictionary<string, deliveryData>>>(responseBodyAsText);

foreach (var delivery in decodedDeliveries)
{
  foreach(var pair in delivery) {
    Console.WriteLine(pair.Key + " : " + pair.Value);
  }
}