Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, here's the skinny. It's an Array!</p> <p>$.isArray(val) will return true and identify it as such, and it can be iterated with $.each(); just like other objects.</p> <p>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.</p> <p>For my needs I changed the first if() in my recusion function from this... </p> <pre><code>if((val == '[object Object]') &amp;&amp; (typeof val == 'object')){ </code></pre> <p>to this...</p> <pre><code>if((val != null) &amp;&amp; (typeof val == 'object') &amp;&amp; ((val == '[object Object]') || (val[0] == '[object Object]'))){ </code></pre> <p>That will match objects, or arrays of objects, then send them back to resurse();</p> <p>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.</p> <p>Thanks for your attention, I figured out my issue, and I've now got an account on Stackoverflow. It's a win-win-win!</p> <p>Thanks again.</p> <p>Skip</p> <hr> <p>Here is the revised buildULfromOBJ(); function...</p> <pre><code>function buildULfromOBJ(obj){ var fragments = []; //declare recursion function function recurse(item){ fragments.push('&lt;ul&gt;'); // start a new &lt;ul&gt; $.each(item, function(key, val) { // iterate through items. if((val != null) &amp;&amp; (typeof val == 'object') &amp;&amp; // catch nested objects ((val == '[object Object]') || (val[0] == '[object Object]'))){ fragments.push('&lt;li&gt;[' + key + '] =&gt;&lt;/li&gt;'); // add '[key] =&gt;' recurse(val); // call recurse to add a nested &lt;ul&gt; }else if(typeof(val)=='string'){ // catch strings, add double quotes fragments.push('&lt;li&gt;[' + key + '] = \"' + val + '\"&lt;/li&gt;'); }else if($.isArray(val)){ // catch arrays add [brackets] fragments.push('&lt;li&gt;[' + key + '] = [' + val + ']&lt;/li&gt;'); }else{ // default: just print it. fragments.push('&lt;li&gt;[' + key + '] = ' + val + '&lt;/li&gt;'); } }); fragments.push('&lt;/ul&gt;'); // close &lt;/ul&gt; } // end recursion function recurse(obj); // call recursion return fragments.join(''); // return results } // end buildULfromJSON() </code></pre> <p>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.</p> <p>Here is the same snippet I posted earlier, properly formatted this time...</p> <pre><code>&lt;snippet&gt; [text] = "#Hashtag @PithyTwits @LuvsIt2 http://link1.com http://link2.com http://link3.com" [retweet_count] = 0 [entities] =&gt; [hashtags] =&gt; [0] =&gt; [text] = "Hashtag" [indices] = [0,8] [user_mentions] =&gt; [0] =&gt; [indices] = [9,20] [screen_name] = "PithyTwits" [name] = "[object Object]" [id_str] = "258175966" [id] = 258175966 [1] =&gt; [indices] = [21,29] [screen_name] = "LuvsIt2" [name] = "Strictly Indifferent" [id_str] = "255883555" [id] = 255883555 [urls] =&gt; [0] =&gt; [indices] = [30,46] [url] = "http://link1.com" [expanded_url] = null [1] =&gt; [indices] = [47,63] [url] = "http://link2.com" [expanded_url] = null [2] =&gt; [indices] = [64,80] [url] = "http://link3.com" [expanded_url] = null [in_reply_to_screen_name] = null &lt;/snippet&gt; </code></pre> <p>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 :) )</p> <p>Thanks again!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload