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.
Coordinator
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:
[HttpGet]
        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 = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + 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 + ".staticflickr.com/" + photo.Server + "/" + photo.PhotoId + "_" + photo.Secret + ".jpg\"></a>");
                //}
                jsonPhotos.Serialize(photoArray);
                HttpRuntime.Cache[cacheKey] = jsonPhotos;
                return jsonPhotos;
            }
            else
            {
                //return data.ToString();
                jsonPhotos.Serialize(data);
                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?
Coordinator
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.

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