Ich bin # Neuling ac und ich herausgefunden, eine Antwort von einer API erhalten mit Httpclient und mit dem folgenden Code, den ich von dem Server die JSON bekam unten gezeigt:Wie man über eine Liste Wörterbücher iteriert und ihren Inhalt druckt?
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-Antwort bis hier:
[ {
"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"
}
} ]
Nun, wenn ich einen kurzen Kommentar zum
Console.WriteLine (responseBodyAsText);
und Kommentar- die Dekoderleitung und die foreach, ist das, was ich bekomme:
System.Collections.Generic.Dictionary`2[System.String,ProjectName.Program+deliveryData]
System.Collections.Generic.Dictionary`2[System.String,ProjectName.Program+deliveryData]
Was ich will, ist einen sauberen Druck der Felder in der JSON gesehen zu bekommen, da mein nächster Schritt ist um jedes Feld der Wörterbücher in einer Access DB zu speichern (was ich nicht weiß, wie ich vorgehen soll, aber ich werde es herausfinden). Also brauche ich ein wenig Hilfe dabei, jeder Rat wird sehr geschätzt.
Vielen Dank im Voraus.
http://stackoverflow.com/questions/14719955/looping-through-dictionary-object – MethodMan
Du Deserialisierung in eine 'List>'? Meinst du nicht 'Dictionary '? Dann zum Ausdrucken seiner nur 'Console.WriteLine (item.key +" "+ item.value);' –
@MethodMan Ich würde persönlich [this] (http://stackoverflow.com/a/141098/6352535) antworten/thread stattdessen –