How to iterate over a list dictionaries and print their content?
我是一个C新手,我发现使用httpclient从API获取响应,并使用以下代码从服务器获取json,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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 ="[email protected]", 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响应:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [ { "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的注释,这就是我得到的:
1 2 | System.Collections.Generic.Dictionary`2[System.String,ProjectName.Program+deliveryData] System.Collections.Generic.Dictionary`2[System.String,ProjectName.Program+deliveryData] |
我想要的是获得JSON中查看的字段的干净打印,因为我的下一步是将字典的每个字段保存在一个AccessDB中(我不知道该怎么做,但我会解决这个问题)。所以我需要一点帮助,任何建议都会引起重视。
非常感谢。
不打印
1 2 3 4 5 6 7 8 | 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); } } |