2011-11-30 114 views
1

我想使用JSon.net反序列化flickr.com photoset(見http://www.flickr.com/services/api/flickr.photosets.getPhotos.html) 的JSON我得到的一個例子是Newtonsoft JSON.NET反序列化到一個類型的對象

{ 
photoset: { 
id: "72154517991528243", 
primary: "6346929005", 
owner: "[email protected]", 
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" 
} 

類我使用的是這些:

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


} 
當我使用它們

反序列化:

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

s將兩個成員都設爲null。

我試圖匹配字符串和整數和列表,但我可能犯了一些錯誤。 謝謝大家!

回答

3

出現了兩個錯誤:在flickrset的性質不公開,這些照片是一個數組不是List <>(不知道爲什麼) 類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; } 


} 
+0

中邦!相反,.net應該像一個智能機器人:) – iMatoria

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