Search code examples
c#jsonjson.netflickr

Newtonsoft JSON.NET deserializing to a typed object


I'd like to use JSon.net to deserialize a flickr.com photoset (see http://www.flickr.com/services/api/flickr.photosets.getPhotos.html ) an example of the JSON I get is

    {
photoset: {
id: "72154517991528243",
primary: "6346929005",
owner: "9999999@N00",
ownername: "myname",
photo: [
{
id: "6340104934",
secret: "18ab51078a",
server: "6106",
farm: 7,
title: "Day #1/30 - homemade kanelbullar",
isprimary: "0"


  } 
.... lots of these photos...
],
page: 1,
per_page: 500,
perpage: 500,
pages: 1,
total: "18"
},
stat: "ok"
}

the classes I am using are these:

class FlickrSet
    {

    Photoset photoset {get;set;}
    string stat{get;set;}
}
class Photoset
{

    public string id { get; set; }
    public string primary { get; set; }
    public string owner { get; set; }
    public string ownername { get; set; }
    public List<Photo> photo { get; set; }
    public int page { get; set; }
    public int per_page { get; set; }
    public int perpage { get; set; }
    public int pages { get; set; }
    public string total { get; set; }
}

class Photo
{
    public string id { get; set; }
    public string secret { get; set; }
    public string server { get; set; }
    public int farm { get; set; }
    public string title { get; set; }
    public string isprimary { get; set; }


}

when I use them to deserialize:

    var s=    JsonConvert.DeserializeObject<FlickrSet>(outPut );

s has both members null.

I've tried to match strings and ints and Lists, but I have probably made some errors. Thanks everyone!


Solution

  • there were two mistakes: the properties on flickrset were not public and the photos are an array not a List<> (don't know why) class FlickrSet {

        **public** Photoset photoset {get;set;}
        **public** string stat{get;set;}
    }
    class Photoset
    {
    
        public string id { get; set; }
        public string primary { get; set; }
        public string owner { get; set; }
        public string ownername { get; set; }
        public **Photo[]** photo { get; set; }
        public int page { get; set; }
        public int per_page { get; set; }
        public int perpage { get; set; }
        public int pages { get; set; }
        public string total { get; set; }
    }
    
    class Photo
    {
        public string id { get; set; }
        public string secret { get; set; }
        public string server { get; set; }
        public int farm { get; set; }
        public string title { get; set; }
        public string isprimary { get; set; }
    
    
    }