You’re absolutely right about it being in XML . However, it’s not parsed to JSON, but to a JS object. So, I took your XML and ran it through xml2js and then converted it into a pretty JSON, so the structure can be seen (remember it’s not in JSON following xml2js, but this makes it easier to understand):
{
"current": {
"city": [{
"$": {
"id": "2158177",
"name": "Melbourne"
},
"coord": [{
"$": {
"lon": "144.96",
"lat": "-37.81"
}
}],
"country": ["AU"],
"sun": [{
"$": {
"rise": "2015-11-24T18:53:52",
"set": "2015-11-25T09:20:59"
}
}]
}],
"temperature": [{
"$": {
"value": "286.15",
"min": "286.15",
"max": "286.15",
"unit": "kelvin"
}
}],
"humidity": [{
"$": {
"value": "43",
"unit": "%"
}
}],
"pressure": [{
"$": {
"value": "1008",
"unit": "hPa"
}
}],
"wind": [{
"speed": [{
"$": {
"value": "9.8",
"name": "Fresh Breeze"
}
}],
"gusts": [{
"$": {
"value": "15.4"
}
}],
"direction": [{
"$": {
"value": "250",
"code": "WSW",
"name": "West-southwest"
}
}]
}],
"clouds": [{
"$": {
"value": "75",
"name": "broken clouds"
}
}],
"visibility": [{
"$": {
"value": "10000"
}
}],
"precipitation": [{
"$": {
"mode": "no"
}
}],
"weather": [{
"$": {
"number": "803",
"value": "broken clouds",
"icon": "04n"
}
}],
"lastupdate": [{
"$": {
"value": "2015-11-25T22:43:00"
}
}]
}
}
So, to get to your “broken clouds”, you need to traverse the object (as returned into temp
by xml2js):
temp.current.clouds[0]['$'].name // "broken clouds"
clouds
is not part of the city
object (which seems odd), but agrees with the XML:
<current>
<city id="2158177" name="Melbourne">
<coord lon="144.96" lat="-37.81"></coord>
<country>AU</country>
<sun rise="2015-11-24T18:53:52" set="2015-11-25T09:20:59"></sun>
</city>
<temperature value="286.15" min="286.15" max="286.15" unit="kelvin"></temperature>
<humidity value="43" unit="%"></humidity>
<pressure value="1008" unit="hPa"></pressure>
<wind>
<speed value="9.8" name="Fresh Breeze"></speed>
<gusts value="15.4"></gusts>
<direction value="250" code="WSW" name="West-southwest"></ direction>
</wind>
<clouds value="75" name="broken clouds"></clouds>
<visibility value="10000"></visibility>
<precipitation mode="no"></precipitation>
<weather number="803" value="broken clouds" icon="04n"></weather>
<lastupdate value="2015-11-25T22:43:00"></lastupdate>
</current>
HTH