使用DataContractJsonSerializer解析Flickr JSON响应

时间:2013-08-24 09:11:26

标签: c# json flickr

所以我试图使用DataContractJsonSerializer从Flickr解析一些JSON数据 我收到JSON响应并将其保存在Stream中没有问题,这意味着我已经通过写入文件传递它来测试它,而JSON就在那里。

jsonFlickrApi({"photos":{"page":1, "pages":372738, "perpage":10, "total":"3727375", "photo":[{"id":"9578613971", "owner":"7960563@N07", "secret":"b7b80b75f8", "server":"3734", "farm":4, "title":"1970 - 1978 Toyota Corolla E20 Coup\u00e9", "ispublic":1, "isfriend":0, "isfamily":0, "url_t":"http:\/\/farm4.staticflickr.com\/3734\/9578613971_b7b80b75f8_t.jpg", "height_t":"67", "width_t":"100", "url_o":"http:\/\/farm4.staticflickr.com\/3734\/9578613971_0eda23bccb_o.jpg", "height_o":"1000", "width_o":"1500"}}]}, "stat":"ok"})

但是当我尝试使用Jsonserializer进行解析时,我发现它不包含任何内容。

我已经通过合同类建立了感谢Json2Cshap.com

public class ResponseContract
{
    [DataContract]
    public class Photo
    {
        [DataMember]
        public string id { get; set; }

        [DataMember]
        public string owner { get; set; }

        [DataMember]
        public string secret { get; set; }

        [DataMember]
        public string server { get; set; }

        [DataMember]
        public int farm { get; set; }

        [DataMember]
        public string title { get; set; }

        [DataMember]
        public string url_t { get; set; }

        [DataMember]
        public string url_o { get; set; }
    }

    [DataContract]
    public class Photos
    {
        [DataMember]
        public int page { get; set; }

        [DataMember]
        public int pages { get; set; }

        [DataMember]
        public int perpage { get; set; }

        [DataMember]
        public string total { get; set; }

        [DataMember]
        public List<Photo> photoList { get; set; }
    }
    [DataContract]
    public class RootObject
    {
        [DataMember]
        public Photos photos { get; set; }
        [DataMember]
        public string stat { get; set; }
    }

我的代码看起来像这样:

            // Creates an HttpWebRequest with the specified URL. 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.longUrl);

        // Send the request and wait for response.          
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Get the response stream
        Stream responseStream = response.GetResponseStream();

        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Photos));
        object objResponse = (Photos)jsonSerializer.ReadObject(responseStream);

        Photos jsonResponse = objResponse as Photos;

        response.Close();

但我在jsonResponse

中什么都没得到

从msdn.microsoft.com/en-us/library/hh674188.aspx获得一些代码 让我完成这一步的一点帮助将非常感激。

1 个答案:

答案 0 :(得分:2)

尝试从最后删除“jsonFlickrApi(”来自JSON start和“)”..

我认为你正在向API发送一个回调参数,这里你不需要,因为你没有使用JavaScript来解析响应