Reputation: 1061
I am trying to parse a JSON file that comes from a Wordpress site, using the JSON-API module. I am using JavaScript, JQuery and JQuery Mobile to display a list of poets from an online poetry magazine that uses Wordpress. I use a GET request that uses the JSON-API Wordpress plugin to get a list of all the poets/authors. This GET request returns with a callback function that parses that JSON. Each poet has a post that serves as their bio page and I attached a photo if one was available. My callback function tries to populate the JQuery Mobile page with the bio and the photo. The photo is available in the json data as a thumbnail attribute (not sure if that is the right word). I get to the thummbnail using dot syntax. Some of the poets do not have a photo, so I tried to have it then display a default image. I am getting an errors with regard to entries that do not have a thumbnail value. It's undefined for some. I check for that with an if statement, but my app still fails due to where the thumbnail is not defined.
The function is available in this pastebin: http://pastebin.com/L8Mb1dCj I start with this:
$.each(data.posts,function(key,val)
then...
if (val.thumbnail !== 'undefined')
What's wrong with that? Why doesn't it handle where thumbnail doesn't exist? Lastly, can this be handled better with a JavaScript template library? Like Mustache? The calling script is below.
Thanks, Bruce
Upvotes: 1
Views: 704
Reputation: 598
function GetJSONifNeeded(data) {
if (typeof data == "object")
return data;
else
return JSON.parse(data);
}
function GetJSONstringfyifNeeded(data) {
if (typeof data == "object")
return JSON.stringify(data);
else
return data;
}
TRY THIS
Upvotes: 1