How to handle comma separated objects in json? ( [object Object],[object Object] )
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 se开发者_如何学Pythonperated 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
OK, here's the skinny. It's an Array!
$.isArray(val) will return true and identify it as such, and it can be iterated with $.each(); just like other objects.
It is a valid object structure, from valid JSON, and can be avoided by using the JSON_FORCE_OBJECT option in PHP's json_encode(); function. For my needs it is better to not force the object because I'm also dealing with arrays of integers I want returned on a single line.
For my needs I changed the first if() in my recusion function from this...
if((val == '[object Object]') && (typeof val == 'object')){
to this...
if((val != null) && (typeof val == 'object') &&
((val == '[object Object]') || (val[0] == '[object Object]'))){
That will match objects, or arrays of objects, then send them back to resurse();
Oddly, Javascript complains when val is null and we test against val[0]. I guess it probably makes sense, because you aren't just testing against the value, your also trying to dive into the null object.
Thanks for your attention, I figured out my issue, and I've now got an account on Stackoverflow. It's a win-win-win!
Thanks again.
Skip
Here is the revised buildULfromOBJ(); function...
function buildULfromOBJ(obj){
var fragments = [];
//declare recursion function
function recurse(item){
fragments.push('<ul>'); // start a new <ul>
$.each(item, function(key, val) { // iterate through items.
if((val != null) && (typeof val == 'object') && // catch nested objects
((val == '[object Object]') || (val[0] == '[object Object]'))){
fragments.push('<li>[' + key + '] =></li>'); // add '[key] =>'
recurse(val); // call recurse to add a nested <ul>
}else if(typeof(val)=='string'){ // catch strings, add double quotes
fragments.push('<li>[' + key + '] = \"' + val + '\"</li>');
}else if($.isArray(val)){ // catch arrays add [brackets]
fragments.push('<li>[' + key + '] = [' + val + ']</li>');
}else{ // default: just print it.
fragments.push('<li>[' + key + '] = ' + val + '</li>');
}
});
fragments.push('</ul>'); // close </ul>
}
// end recursion function
recurse(obj); // call recursion
return fragments.join(''); // return results
} // end buildULfromJSON()
The top two elses are simply to make pretty output, and help deliniate between strings and literals. They can be removed to streamline the flow.
Here is the same snippet I posted earlier, properly formatted this time...
<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] =>
[0] =>
[indices] = [9,20]
[screen_name] = "PithyTwits"
[name] = "[object Object]"
[id_str] = "258175966"
[id] = 258175966
[1] =>
[indices] = [21,29]
[screen_name] = "LuvsIt2"
[name] = "Strictly Indifferent"
[id_str] = "255883555"
[id] = 255883555
[urls] =>
[0] =>
[indices] = [30,46]
[url] = "http://link1.com"
[expanded_url] = null
[1] =>
[indices] = [47,63]
[url] = "http://link2.com"
[expanded_url] = null
[2] =>
[indices] = [64,80]
[url] = "http://link3.com"
[expanded_url] = null
[in_reply_to_screen_name] = null
</snippet>
Notice that [entities][user_mentions][0][name] = "[object Object]" I stuck that in to ensure that string values don't break the code. Also notice the [indices] items. Those are the arrays I prefer to see on a single line (I get anal over the stupidest stuff :) )
Thanks again!
精彩评论