I’m receiving data from the GMail API using $.ajax() with dataType: "xml", then throwing it into $.xml2json, which is a jQuery plugin. The problem is (as ironically demonstrated by the demo on the page) that when I put in something like this:
<animals>
<dog>
<name>Rufus</name>
<breed>labrador</breed>
</dog>
<dog>
<name>Marty</name>
<breed>whippet</breed>
</dog>
<cat name="Matilda"/>
</animals>
I get this:
{
animals: {
dog: [
{
name: 'Rufus',
breed: 'labrador'
}, {
name: 'Marty',
breed: 'whippet'
}
],
cat: {
name: 'Matilda'
}
}
}
Anyway, as you can see, in <animals> there are two <dog>s and only one <cat>; therefore, animals["cat"] is an object, while animals["dog"] is an array of objects. Likewise, when there is only one <entry> returned by GMail, feed["entry"] is an array of what is inside it, but when there is more than one tag, feed["entry"] is an array of entries, which in turn are arrays of what is inside them.
What I want to do is do different things, depending on whether I have 1 or more entries, using a conditional statement; to do that I need a boolean which tells me how many entries there are. How would I go about doing that? JQuery welcome.
EDIT: I guess another possible answer would be to edit the source of the plugin itself and make it offer some sort of way of knowing if it is a lone tag (an array-like object) or if there are lots of them (a "true" array).
You can use jQuery
type()method to streamline your code and take advantage of fully cross browser compliant testsAPI Refernce : http://api.jquery.com/jQuery.type/