-1

So i have this JSON Object coming from a HTTP request

[
  {
    "location": {
      "name": "Seattle, WA",
      "lat": "47.604",
      "long": "-122.329",
      "timezone": "-7",
      "alert": "",
      "degreetype": "F",
      "imagerelativeurl": "http:\/\/blob.weather.microsoft.com\/static\/weather4\/en-us\/"
    },
    "current": {
      "temperature": "81",
      "skycode": "32",
      "skytext": "Sunny",
      "date": "2015-08-09",
      "observationtime": "18:00:00",
      "observationpoint": "Seattle, WA",
      "feelslike": "81",
      "humidity": "39",
      "winddisplay": "3 mph Northwest",
      "day": "Sunday",
      "shortday": "Sun",
      "windspeed": "3 mph",
      "imageUrl": "http:\/\/blob.weather.microsoft.com\/static\/weather4\/en-us\/law\/32.gif"
    },
    "forecast": [
      {
        "low": "60",
        "high": "81",
        "skycodeday": "29",
        "skytextday": "Partly Cloudy",
        "date": "2015-08-08",
        "day": "Saturday",
        "shortday": "Sat",
        "precip": ""
      },
      {
        "low": "65",
        "high": "82",
        "skycodeday": "34",
        "skytextday": "Mostly Sunny",
        "date": "2015-08-09",
        "day": "Sunday",
        "shortday": "Sun",
        "precip": "0"
      },
      {
        "low": "66",
        "high": "82",
        "skycodeday": "32",
        "skytextday": "Sunny",
        "date": "2015-08-10",
        "day": "Monday",
        "shortday": "Mon",
        "precip": "0"
      },
      {
        "low": "68",
        "high": "83",
        "skycodeday": "30",
        "skytextday": "Partly Sunny",
        "date": "2015-08-11",
        "day": "Tuesday",
        "shortday": "Tue",
        "precip": "0"
      },
      {
        "low": "65",
        "high": "88",
        "skycodeday": "32",
        "skytextday": "Sunny",
        "date": "2015-08-12",
        "day": "Wednesday",
        "shortday": "Wed",
        "precip": "0"
      }
    ]
  }
]

and this is my current code to run it

               var responseObject = JSON.parse(xhr.responseText);
                var newContent= '';
               for(var i = 0; i < xhr.responseText.length;i++){
                        newContent += '<div class = "location">';

    //newContent += '<p><b>'+xhr.responseText[i].location.pid + '</b><br>';

    newContent += '<p> Name: <b>' + xhr.responseText.location[i].location +  '</b><br>';
   newContent += '<p>Lat: <b>' + responseObject.location[i].lat + '</b><br>';
   newContent += '<p>Long: <b>' + responseObject.location[i].long 
      + '</b>    <br>';
   newContent += '<p>TimeZone: <b>' + responseObject.location[i].timezone +  
       '</b><br>';
                        newContent += '<p>Alert: <b>' + responseObject.location[i].alert + '</b><br>';
                        newContent += '<p>degreeType: <b>' + responseObject.location[i].degreeType + '</b><br>';
                        newContent += '</div>';
                    }

The Problem I am having is i can that there are 3 different objects within 1 JSON object. How do I parse through it? I am very new at parsing JSON

Andrey
  • 59,039
  • 12
  • 119
  • 163
Ali Aslam
  • 115
  • 1
  • 9
  • responseObject is array, iterate over it. also this is not really JSON, because it is array (O in JSON is for Object) – Andrey Aug 10 '15 at 02:55
  • 1
    In general, see [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). – Jonathan Lonowski Aug 10 '15 at 02:56
  • @Andrey Note: JSON doesn't require an Object as the root of the data. [It can be any "*Value*"](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf), including Arrays, strings, etc. An Object or Array is only required to allow for nesting values. – Jonathan Lonowski Aug 10 '15 at 02:58
  • @JonathanLonowski thanks, didn't know. – Andrey Aug 10 '15 at 03:00
  • Typo: The loop is currently counting over the characters in `xhr.responseText` rather than iterating the parsed Array of Objects in `responseObject` (though, in different places, using the same iterator variable for both). – Jonathan Lonowski Aug 10 '15 at 03:06

1 Answers1

0

This how would you parse through the response array that you got.

var responseObject = JSON.parse(xhr.responseText),
    newContent = '',
    location = responseObject[0].location,
    current = responseObject[0].current,
    forecast = responseObject[0].forecast;

newContent += '<div class = "location">';
newContent += '<p> Name: <b>' + location.name + '</b><br>';
newContent += '<p>Lat: <b>' + location.lat + '</b><br>';
newContent += '<p>Long: <b>' + location.long + '</b>    <br>';
newContent += '<p>TimeZone: <b>' + location.timezone + '</b><br>';
newContent += '<p>Alert: <b>' + location.alert + '</b><br>';
newContent += '<p>degreeType: <b>' + location.degreeType + '</b><br>';
newContent += '</div>';
Allan Chua
  • 9,305
  • 9
  • 41
  • 61