我交流#新手,我想通了,讓使用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 /線程 –