簡體   English   中英

Newtonsoft JSON.NET反序列化為類型化對象

[英]Newtonsoft JSON.NET deserializing to a typed object

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

    {
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"
}

我正在使用的類是這些:

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。

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

有兩個錯誤:flickrset上的屬性未公開,並且照片是數組,而不是List <>(不知道為什么)。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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM