Guchen
Guchen

Reputation: 3

C# get children from JSON

my JSON looks like this:

 {
   "kind": "youtube#videoListResponse",
   "etag": "\"sZ5p5Mo8dPpfIzLYQBF8QIQJym0/TOPzMxlQJUtRJBXHeKYMXsdEkHs\"",
   "pageInfo": 
   {
     "totalResults": 1,
     "resultsPerPage": 1
   },
   "items": 
     [
       {
         "kind": "youtube#video",
         "etag": "\"sZ5p5Mo8dPpfIzLYQBF8QIQJym0/BIVqr1Mkbule8othzWvZRor92wU\"",
         "id": "QMNkWwq6L4Q",
         "contentDetails": 
         {
           "duration": "PT4M45S",
           "dimension": "2d",
           "definition": "hd",
           "caption": "false",
           "licensedContent": true,
           "projection": "rectangular"
         }
       }
     ]
}

The formatting might be a bit off, sorry. I tried creating a class like this:

public class VideoDetails
{
    public string kind;
    public string etag;
    public string id;
    public string duration;
    public string definition;
    public string caption;
    public string licensedContent;
    public string projection;
}

After that I deserialize the JSON file:

VideoDetailRead = JsonConvert.DeserializeObject<VideoDetails>(json);

but I only get "etag" and "kind". Nothing else. My question is: How do I read the data that's listed under "items"? This is my first time working with JSON and therefor I don't know much. Thanks for some answers.

Upvotes: 0

Views: 3113

Answers (4)

jorgonor
jorgonor

Reputation: 1719

Create a second object to model the children data. Provided the items property has also a child under contentDetails, you will also need another object to deserialize these properties.

public class VideoDetailsItem
{
     public string kind;
     public string etag;
     public string id;
     public VideoContentDetails contentDetails;
}

public class VideoContentDetails
{
     public string duration;
     public string definition;
     public string caption;
     public bool licensedContent;
     public string projection;
}

And to the parent object add a List<VideoDetailsItem>.

public class VideoDetails
{
    public string kind;
    public string etag;
    public List<VideoDetailsItem> items;
}

When deserializing JSON objects you have to mimic the JSON object structure in your object. Also, consider using properties instead of public fields to favor encapsulation in your data objects.

Upvotes: 3

DrewJordan
DrewJordan

Reputation: 5314

You have to define your class to match the structure of the JSON: in this case, you would define items as a list of a separate class which has the properties under it like duration etc. You should have your VideoDetails class nested inside another class that can wrap both objects in the JSON, and add another class for the contentDetails object.

One thing you can do is copy the JSON into your clipboard, open a new class file in Visual Studio, and go to Edit > Paste Special > Paste JSON as Classes. This will create the correct structure for you, and you can see how the nested classes in the JSON properly translate to C# classes.

Upvotes: 0

Dudemanword
Dudemanword

Reputation: 704

This is because you aren't properly modeling the object structure. Right now, your JSON is structured like:

{baseInformation, VideoDetailsArray}

Thus you have to restructure your data object as

public class VideoDetails 
{
    //Base information
    public string kind {get; set;}
    public string etag {get; set;}
    //...
    public List<VideoDetails> {get; set;}
}

Upvotes: 0

JLRishe
JLRishe

Reputation: 101768

The properties you have listed are inside an array named items, so your object hierarchy has to match this:

public class VideoList
{
    public string kind;
    public string etag;
    // etc...
    public List<VideoDetails> items;
}

public class VideoDetails
{
    public string kind;
    public string etag;
    public string id;
    public string duration;
    public string definition;
    public string caption;
    public string licensedContent;
    public string projection;
}

Then:

var videos = JsonConvert.DeserializeObject<VideoList>(json);

Upvotes: 0

Related Questions