简体   繁体   中英

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!

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; }


}
System.Web.Script.Serialization.JavaScriptSerializer js = new system.Web.Script.Serialization.JavaScriptSerializer();
return js.Deserialize<FlickrSet>(value);

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