Search code examples
c#jsonflickr

Parsing Flickr JSON Responses with DataContractJsonSerializer


So I'm trying to parse some JSON data from Flickr with using DataContractJsonSerializer I'm receiving the JSON response and keeping it in a Stream no problems, meaning I have tested it by passing it by writing to file and the JSON is all there.

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

But when I try to parse using the Jsonserializer instead I notice that it doesn't contain anything.

I have established by contract class thanks to 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; }
    }

And my code looks like this:

            // 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();

But I get nothing in jsonResponse

Got some of the code from msdn.microsoft.com/en-us/library/hh674188.aspx A little help to get me over this step would be much appreciated.


Solution

  • try removing "jsonFlickrApi(" from JSON start and ")" from the end..

    I think you are sending a callback param to API which you don't need here, since you are not using JavaScript to parse the response