我交流#新手,我想通了,让使用HttpClient的,并用下面的代码我得到了来自服务器的JSON如下所示的API的响应:如何遍历列表字典并打印其内容?
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响应,直到在这里:
[ {
"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数据库中保存字典的每个字段(我不知道该怎么做,但我会弄清楚)。所以我需要一点帮助,任何建议都会非常赞赏。
非常感谢提前。
http://stackoverflow.com/questions/14719955/looping-through-dictionary-object – MethodMan
你反序列化到一个'名单<词典<字符串,deliverydata >>'?你不是指'Dictionary'?然后打印它的只是'Console.WriteLine(item.key +“”+ item.value);' –
@MethodMan我个人建议[this](http://stackoverflow.com/a/141098/6352535)answer /线程 –