0

I'm working with an API with an example output of the following

{
        "id": 12345678

        "photo-url-1280": "http://68.media.tumblr.com/5076e813bdc463409f59172b2c152487/tumblr_ocy8peMYvo1uur5s9o1_1280.png",
        "photo-url-500": "http://67.media.tumblr.com/5076e813bdc463409f59172b2c152487/tumblr_ocy8peMYvo1uur5s9o1_500.png",
        "photo-url-400": "http://68.media.tumblr.com/5076e813bdc463409f59172b2c152487/tumblr_ocy8peMYvo1uur5s9o1_400.png",
        "photo-url-250": "http://67.media.tumblr.com/5076e813bdc463409f59172b2c152487/tumblr_ocy8peMYvo1uur5s9o1_250.png",
        "photo-url-100": "http://67.media.tumblr.com/5076e813bdc463409f59172b2c152487/tumblr_ocy8peMYvo1uur5s9o1_100.png",
        "photo-url-75": "http://67.media.tumblr.com/5076e813bdc463409f59172b2c152487/tumblr_ocy8peMYvo1uur5s9o1_75sq.png",
}

Those last items are very related, so I'd like to move them into their own object.

As of now, getting a specified image would be super messy. Getting the 400px version might look like this -

myPhotoObject.Photo400

However, if I was able to move these URLs into an object of their own, I could more cleanly call it like the following:

myPhotoObject.Photo.400

or even introduce more friendly methods

myPhoto.Photo.GetClosestTo(451);

There is a pattern in the URLs here - after an _ character, the identifying size is shown e.g. ..._1280.png and ..._500.png

Would the best way to get these be to write a getter property that just appends a list of known sizes to a URL? Would this way be any less/more efficient than using purely the JSON.net converters?

Alexander Lozada
  • 4,019
  • 3
  • 19
  • 41

1 Answers1

0

I suggest to parse your JSON into a dictionary first, as described here, and then manually copy it to a more structured object:

Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
var myImageObject = new { Id = "", Images = new Dictionary<int, string>()};
foreach(var key in dict.Keys) {
    if(key == "id") {
         myImageObject.Id = dict[key]
    }
    else {
         var imageSize = ParseForImageSize(key);
         myImageObject.Images.Add(imageSize, dict[key])
    }
}
Community
  • 1
  • 1