This project has moved. For the latest updates, please go here.

PhotosetPhotoCollection Return as JSON

Topics: Developer Forum
Dec 2, 2013 at 10:38 AM
Hi all,

At the moment I have grabbed the photos from Flickr into a PhotosetPhotoCollection which I want to return to the client and parse client side using jQuery.

Currently I've looped through the photo collection and appending a new photo item to a stringbuilder object. I then return this as a string which means almost nothing left to do at client end. But, it also means that in order to make any amendments to the HTML, it has to be done in C# and recompiled which isn't an ideal solution.

I can't seem to find a way to return this data as JSON but I'm sure there must be a way.

Any help much appreciated.
Dec 2, 2013 at 11:27 AM
Surely you just need to use a Json Serializer (such as NewtonSoft.Json) to serialize the object to Json?
Dec 2, 2013 at 12:49 PM
Hi Sam,

Yes I'd have thought so too but it doesn't seem to work.
Here's my code:
        public object GetPhotos()
            var cacheKey = "Gymnastics.FlickrData";
            JavaScriptSerializer jsonPhotos = new JavaScriptSerializer();
            var data = (JavaScriptSerializer)HttpRuntime.Cache[cacheKey];
            var apiKey = ConfigurationManager.AppSettings["FlickrApiKey"];
            var photosetId = ConfigurationManager.AppSettings["FlickrPhotosetId"];
            var requestUrl = "" + apiKey + "&photoset_id=" + photosetId + "&format=json";

            Flickr flickr = new Flickr();
            flickr.InstanceCacheDisabled = true;
            flickr.ApiKey = apiKey;
            if (data == null)
                PhotosetPhotoCollection photoArray = flickr.PhotosetsGetPhotos(photosetId);
                //StringBuilder picture = new StringBuilder();
                //var url = "http://farm";
                //foreach (var photo in photoList)
                //    picture.Append("<a class=\"rsImg\" href=\"" + url + photo.Farm + "" + photo.Server + "/" + photo.PhotoId + "_" + photo.Secret + ".jpg\"></a>");
                HttpRuntime.Cache[cacheKey] = jsonPhotos;
                return jsonPhotos;
                //return data.ToString();
                return jsonPhotos;
I appreciate it could be something I'm doing wrong but the response I get is {"MaxJsonLength":2097152,"RecursionLimit":100}
I've tried increasing the MaxJsonLength to Int32.MaxValue but no success

Any ideas?
Dec 2, 2013 at 3:56 PM
Edited Dec 2, 2013 at 3:57 PM
jsonPhotos.Serialize returns a string - you don't then return the jsonPhotos object itself. Also, if you are using MVC THen you should be returning a JsonResult.

Cache[cacheKey] = photoArray;
return Json(photoArray, JsonRequestBehavior.AllowGet);