I'm new to this so please bear with me. I'm dealing with the Twitter API which relies heavily on JSON (and that's not a bad thing).
Something that Twitter does is return some values as comma seperated objects. This seems to be isolated to the [entities] section, which enumerates links within a tweet.
This is a function I'm using to reduce json to unordered lists. The section commented out of this code is what I need help with.
function buildULfromJSON(data){
var items = [];
function recurse(json){ //declare recursion function
items.push('<ul>'); // start a new <ul>
$.each(json, function(key, val) { // iterate through json and test, treat vals accordingly
if((val == '[object Object]') && (typeof val == 'object')){ // this catches nested json lists
items.push('<li>[' + key + '] =></li>'); // adds '[key] =>'
recurse(val); // calls recurse() to add a nested <ul>
}else if(typeof(val)=='string'){ // catch string values
items.push('<li>[' + key + '] = \"' + val + '\"</li>'); // add double quotes to <li> item
}/* else if(typeof(val) == 'object'){ // this throws exception in jquery.js
$.each(val, function(){ // when val contains [object Object],[object Object]
items.push('<li>[' + key + '] =></li>'); // need to test for and process
recurse(val); // comma seperated objects
});
} */else{
items.push('<li>[' + key + '] = ' + val + '</li>'); // non string, non object data (null, true, 123, .etc)
}
});
items.push('</ul>'); // close </ul>
} // end recursion function
recurse(data); // call recursion
return items.join(''); // return results
} // end buildULfromJSON()
Here is snippet of example output where the tweet contains one hashtag, two user mentions, and three urls. Those items are returned as a comma seperated list of objects (json data). I'm at a complete loss how to tackle this. Any direction you guys could provide would be excellent.
Note the [text] string. It helps to put the [entities] section in context.
<snippet>
[text] = "#Hashtag @PithyTwits @LuvsIt2 http://link1.com http://link2.com http://link3.com"
[retweet_count] = 0
[entities] =>
[hashtags] =>
[0] =>
[text] = "Hashtag"
[indices] = 0,8
[user_mentions] = [object Object],[object Object]
[urls] = [object Object],[object Object],[object Object]
[in_reply_to_screen_name] = null
[in_reply_to_status_id_str] = null
</snippet>
My big issue at this point is that I don't know how to test for these lists without giving jquery.js fits. Once I do know how to isolate these lists in code, string functions for dealing with comma separated lists don't sound like the perfect fit... Any advice would be welcomed.
Thank You,
Skip