2011-11-30 79 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); 
相关问题