2016-10-28 5 views
0

Also ich rufe den LinkedIn-API die Profildaten zu erhalten und es ruft eine JSON.Abrufen der Informationen aus einem verschachtelten JSON Wert

{ 
    "firstName": "Cristian Viorel", 
    "headline": ".NET Developer", 
    "location": { 
    "country": {"code": "dk"}, 
    "name": "Northern Region, Denmark" 
    }, 
    "pictureUrls": { 
    "_total": 1, 
    "values": ["https://media.licdn.com/mpr/mprx/0_PXALDpO4eCHpt5z..."] 
    } 
} 

Ich kann student.firstname, student.headline verwenden. Wie kann ich den Namen des Standortes oder den Wert von pictureUrl abrufen? Etwas wie student.location.name oder student.pictureUrls.values?

+0

Haben Sie Ihre Vorschläge ausprobiert? –

Antwort

3

Ziemlich einfach mit Json.Net. Sie definieren zuerst Ihr Modell:

public class Country 
{ 
    public string code { get; set; } 
} 

public class Location 
{ 
    public Country country { get; set; } 
    public string name { get; set; } 
} 

public class PictureUrls 
{ 
    public int _total { get; set; } 
    public List<string> values { get; set; } 
} 

public class JsonResult 
{ 
    public string firstName { get; set; } 
    public string headline { get; set; } 
    public Location location { get; set; } 
    public PictureUrls pictureUrls { get; set; } 
} 

Dann analysieren Sie einfach Ihre Json Daten:

string json = @"{ 
     'firstName': 'Cristian Viorel', 
     'headline': '.NET Developer', 
     'location': { 
     'country': {'code': 'dk'}, 
     'name': 'Northern Region, Denmark' 
     }, 
     'pictureUrls': { 
     '_total': 1, 
     'values': ['https://media.licdn.com/mpr/mprx/0_PXALDpO4eCHpt5z...'] 
     } 
    }"; 

JsonResult result = JsonConvert.DeserializeObject<JsonResult>(json); 

Console.WriteLine(result.location.name); 

foreach (var pictureUrl in result.pictureUrls.values) 
    Console.WriteLine(pictureUrl); 
+0

es funktioniert! Vielen Dank Alex! – crystyxn

0

Für den Namen ja, aber für Bild benötigen Sie eine for-Schleife oder wenn Sie nur das erste Element student.pictureUrls.values ​​[0] (Werte scheint ein Array sein).

Verwandte Themen