JSON在具有多个对象的javascript中进行解析

时间:2015-08-10 02:52:53

标签: javascript json

所以我有来自HTTP请求的这个JSON对象

[
  {
    "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"
      }
    ]
  }
]

这是我目前运行它的代码

               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>';
                    }

我遇到的问题是我可以在1个JSON对象中有3个不同的对象。我如何解析它?我是解析JSON的新手

1 个答案:

答案 0 :(得分:0)

这将如何解析你得到的响应数组。

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>';