2017-10-07 61 views
-2

嘿傢伙什麼是從這個json字符串中提取專輯id的最佳方式,我已經將它轉換爲JObject但是我堅持不久,使用一些幫助。 ex。第一張專輯的ID我想是「ID」:「0v94MG5nbp4w6xdeFkfQrA」,並有5張專輯在這裏,所以我想5張專輯解析Json獲取所有5個專輯的專輯ID

{ 
"albums" : { 
"href" : "https://api.spotify.com/v1/search?query=Farruko&type=album&market=US&offset=0&limit=5", 
"items" : [ { 
    "album_type" : "album", 
    "artists" : [ { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/329e4yvIujISKGKz1BZZbO" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/329e4yvIujISKGKz1BZZbO", 
    "id" : "329e4yvIujISKGKz1BZZbO", 
    "name" : "Farruko", 
    "type" : "artist", 
    "uri" : "spotify:artist:329e4yvIujISKGKz1BZZbO" 
    } ], 
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY", "VN" ], 
    "external_urls" : { 
    "spotify" : "https://open.spotify.com/album/0v94MG5nbp4w6xdeFkfQrA" 
    }, 
    "href" : "https://api.spotify.com/v1/albums/0v94MG5nbp4w6xdeFkfQrA", 
    "id" : "0v94MG5nbp4w6xdeFkfQrA", 
    "images" : [ { 
    "height" : 640, 
    "url" : "https://i.scdn.co/image/61956518b48663e10775869d8dd4cffbe6f43ac8", 
    "width" : 640 
    }, { 
    "height" : 300, 
    "url" : "https://i.scdn.co/image/6786555cdac766d6b42411f2a54d40b768470492", 
    "width" : 300 
    }, { 
    "height" : 64, 
    "url" : "https://i.scdn.co/image/22d590419d90c411eba2d23561df223dc4e9907e", 
    "width" : 64 
    } ], 
    "name" : "TrapXficante", 
    "type" : "album", 
    "uri" : "spotify:album:0v94MG5nbp4w6xdeFkfQrA" 
}, { 
    "album_type" : "album", 
    "artists" : [ { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/329e4yvIujISKGKz1BZZbO" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/329e4yvIujISKGKz1BZZbO", 
    "id" : "329e4yvIujISKGKz1BZZbO", 
    "name" : "Farruko", 
    "type" : "artist", 
    "uri" : "spotify:artist:329e4yvIujISKGKz1BZZbO" 
    } ], 
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY", "VN" ], 
    "external_urls" : { 
    "spotify" : "https://open.spotify.com/album/1zFp5Q3Dgo8wmKsmfZ23QX" 
    }, 
    "href" : "https://api.spotify.com/v1/albums/1zFp5Q3Dgo8wmKsmfZ23QX", 
    "id" : "1zFp5Q3Dgo8wmKsmfZ23QX", 
    "images" : [ { 
    "height" : 640, 
    "url" : "https://i.scdn.co/image/617c7edb6b107e7dce12f42a9f3cc222046c0adc", 
    "width" : 640 
    }, { 
    "height" : 300, 
    "url" : "https://i.scdn.co/image/a8b199f9a155388b4d4ab31ae96806649154ebdb", 
    "width" : 300 
    }, { 
    "height" : 64, 
    "url" : "https://i.scdn.co/image/2f1d06be5e5ba87732962717e743fba730708d07", 
    "width" : 64 
    } ], 
    "name" : "Visionary", 
    "type" : "album", 
    "uri" : "spotify:album:1zFp5Q3Dgo8wmKsmfZ23QX" 
}, { 
    "album_type" : "album", 
    "artists" : [ { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/329e4yvIujISKGKz1BZZbO" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/329e4yvIujISKGKz1BZZbO", 
    "id" : "329e4yvIujISKGKz1BZZbO", 
    "name" : "Farruko", 
    "type" : "artist", 
    "uri" : "spotify:artist:329e4yvIujISKGKz1BZZbO" 
    } ], 
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY", "VN" ], 
    "external_urls" : { 
    "spotify" : "https://open.spotify.com/album/3nXlHW1yqEnYKfi70zjeK4" 
    }, 
    "href" : "https://api.spotify.com/v1/albums/3nXlHW1yqEnYKfi70zjeK4", 
    "id" : "3nXlHW1yqEnYKfi70zjeK4", 
    "images" : [ { 
    "height" : 640, 
    "url" : "https://i.scdn.co/image/874771d69c2b0df657bbd36998fc5deb75f920aa", 
    "width" : 640 
    }, { 
    "height" : 300, 
    "url" : "https://i.scdn.co/image/b34224e47ebd44ea2c2ea92a38b3d203f2f96907", 
    "width" : 300 
    }, { 
    "height" : 64, 
    "url" : "https://i.scdn.co/image/7a49b98bbe3d512d8d2ed204a823535e3fce817c", 
    "width" : 64 
    } ], 
    "name" : "Farruko Presents Los Menores", 
    "type" : "album", 
    "uri" : "spotify:album:3nXlHW1yqEnYKfi70zjeK4" 
}, { 
    "album_type" : "single", 
    "artists" : [ { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/329e4yvIujISKGKz1BZZbO" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/329e4yvIujISKGKz1BZZbO", 
    "id" : "329e4yvIujISKGKz1BZZbO", 
    "name" : "Farruko", 
    "type" : "artist", 
    "uri" : "spotify:artist:329e4yvIujISKGKz1BZZbO" 
    }, { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/4q3ewBCX7sLwd24euuV69X" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/4q3ewBCX7sLwd24euuV69X", 
    "id" : "4q3ewBCX7sLwd24euuV69X", 
    "name" : "Bad Bunny", 
    "type" : "artist", 
    "uri" : "spotify:artist:4q3ewBCX7sLwd24euuV69X" 
    }, { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/1i8SpTcr7yvPOmcqrbnVXY" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/1i8SpTcr7yvPOmcqrbnVXY", 
    "id" : "1i8SpTcr7yvPOmcqrbnVXY", 
    "name" : "Ozuna", 
    "type" : "artist", 
    "uri" : "spotify:artist:1i8SpTcr7yvPOmcqrbnVXY" 
    } ], 
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY", "VN" ], 
    "external_urls" : { 
    "spotify" : "https://open.spotify.com/album/5wtK6rD6OVbGxxiQ7gzgfD" 
    }, 
    "href" : "https://api.spotify.com/v1/albums/5wtK6rD6OVbGxxiQ7gzgfD", 
    "id" : "5wtK6rD6OVbGxxiQ7gzgfD", 
    "images" : [ { 
    "height" : 600, 
    "url" : "https://i.scdn.co/image/c011680190a6e2273de0ede0e19fd1e2b4f60cd7", 
    "width" : 600 
    }, { 
    "height" : 300, 
    "url" : "https://i.scdn.co/image/4c1557e949155fe75563ac14862e45388722ffa3", 
    "width" : 300 
    }, { 
    "height" : 64, 
    "url" : "https://i.scdn.co/image/b0bfe473cdc9c2c1de7c7204986797be0592c8ea", 
    "width" : 64 
    } ], 
    "name" : "Diles", 
    "type" : "album", 
    "uri" : "spotify:album:5wtK6rD6OVbGxxiQ7gzgfD" 
}, { 
    "album_type" : "single", 
    "artists" : [ { 
    "external_urls" : { 
     "spotify" : "https://open.spotify.com/artist/2R21vXR83lH98kGeO99Y66" 
    }, 
    "href" : "https://api.spotify.com/v1/artists/2R21vXR83lH98kGeO99Y66", 
    "id" : "2R21vXR83lH98kGeO99Y66", 
    "name" : "Anuel Aa", 
    "type" : "artist", 
    "uri" : "spotify:artist:2R21vXR83lH98kGeO99Y66" 
    } ], 
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY", "VN" ], 
    "external_urls" : { 
    "spotify" : "https://open.spotify.com/album/2ucmNNr2Y9ZI7fP4oAMdIj" 
    }, 
    "href" : "https://api.spotify.com/v1/albums/2ucmNNr2Y9ZI7fP4oAMdIj", 
    "id" : "2ucmNNr2Y9ZI7fP4oAMdIj", 
    "images" : [ { 
    "height" : 640, 
    "url" : "https://i.scdn.co/image/03053a17a55677759afe8ffa9745f4da3b9228d6", 
    "width" : 640 
    }, { 
    "height" : 300, 
    "url" : "https://i.scdn.co/image/c4c377376efe405009fedbc2deb829b5a7d348b7", 
    "width" : 300 
    }, { 
    "height" : 64, 
    "url" : "https://i.scdn.co/image/f55f20474ebebabf8816a5474ce9896978773c39", 
    "width" : 64 
    } ], 
    "name" : "Sola (Remix) [feat. Daddy Yankee, Wisin, Farruko, Zion & Lennox]", 
    "type" : "album", 
    "uri" : "spotify:album:2ucmNNr2Y9ZI7fP4oAMdIj" 
} ], 
"limit" : 5, 
"next" : "https://api.spotify.com/v1/search?query=Farruko&type=album&market=US&offset=5&limit=5", 
"offset" : 0, 
"previous" : null, 
"total" : 150 
} 
} 

回答

1

可以反序列化到具體的類.. See how those class are generated

var rootObj = JsonConvert.DeserializeObject<SOTest.RootObject>(json); 
foreach(var item in rootObj.albums.items) 
{ 
    Console.WriteLine(item.id); 
} 

public class SOTest 
{ 
    public class ExternalUrls 
    { 
     public string spotify { get; set; } 
    } 

    public class Artist 
    { 
     public ExternalUrls external_urls { get; set; } 
     public string href { get; set; } 
     public string id { get; set; } 
     public string name { get; set; } 
     public string type { get; set; } 
     public string uri { get; set; } 
    } 


    public class Image 
    { 
     public int height { get; set; } 
     public string url { get; set; } 
     public int width { get; set; } 
    } 

    public class Item 
    { 
     public string album_type { get; set; } 
     public List<Artist> artists { get; set; } 
     public List<string> available_markets { get; set; } 
     public ExternalUrls external_urls { get; set; } 
     public string href { get; set; } 
     public string id { get; set; } 
     public List<Image> images { get; set; } 
     public string name { get; set; } 
     public string type { get; set; } 
     public string uri { get; set; } 
    } 

    public class Albums 
    { 
     public string href { get; set; } 
     public List<Item> items { get; set; } 
     public int limit { get; set; } 
     public string next { get; set; } 
     public int offset { get; set; } 
     public object previous { get; set; } 
     public int total { get; set; } 
    } 

    public class RootObject 
    { 
     public Albums albums { get; set; } 
    } 
} 
+0

謝謝你的回答幫助我瞭解發生了什麼事 – Yaboy93