简体   繁体   中英

deserializing JSON to .net object using NewtonSoft - array

{
  "albums": [
    {
      "name": "Muse",
      "permalink": "Muse",
      "cover_image_url": "http://image.kazaa.com/images/69/01672812 1569/Yaron_Herman_Trio/Muse/Yaron_Herman_Trio-Muse_1.jpg",
      "id": 93098,
      "artist_nam e": "Yaron Herman Trio"
    },

}

How do i get the value of 'name' contained in 'album'? Please help! Are there any specific ways to do it? Looked through the api and tried but stuck at retrieving the value!

Just Deserialized your json into your object.

using JsonConvert.DeserializeObject<objectType>(jsonString);

eg; you have a class

Public class Album
{
public string name {get;set;}
public string permalink {get;set;},
public string cover_image_url {get;set;}
public int id {get;set;}
public string artist_name{get;set}
}

another class

public class Albums
{
List<Album> albums{get;set;}
}

Then just use

 var albums=JsonConvert.DeserializeObject<Albums>(jsonString);

Now albums contains the list of album objects so now you can get any value from there.

Make sure your collection variable is declared public. For example:

public class Albums
{
   public List<Album> albums{get;set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM