-6

如何获取json值并在javascript中解析并输出到html

$.getJSON(
{
  "current_observation": {
    "credit": "Weather Underground NOAA Weather Station",
    "credit_URL": "wunderground.com/",
    "termsofservice": {
      "-link": "wunderground.com/weather/api/d/terms.html",
          "#text": "

This feed will be deprecated. Please switch to wunderground.com/weather/api/
"
    },
    "image": {
      "url": "
http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png
",
      "title": "Weather Underground",
      "link": "http://wunderground.com/"
    },

    "display_location": {
      "full": "Chetput, India",
      "city": "Chetput",
      "state_name": "India",
      "country": "IN",
      "country_iso3166": "IN",
      "zip": "00000",
      "latitude": "13.060420",
      "longitude": "80.249580",
      "elevation": "11.00000000 ft"
    },
    "observation_location": {
      "full": "Chennai,",
      "city": "Chennai",
      "country": "IN",
      "country_iso3166": "IN",
      "latitude": "13.00000000",
      "longitude": "80.18000031",
      "elevation": "52 ft"
    },
    "station_id": "VOMM",
    "observation_time": "Last Updated on December 11, 4:10 PM IST",
    "observation_time_rfc822": "Tue, 11 Dec 2012 10:40:00 GMT",
    "observation_epoch": "1355222400",
    "local_time": "December 11, 4:43 PM IST",
    "local_time_rfc822": "Tue, 11 Dec 2012 11:13:29 GMT",
    "local_epoch": "1355224409",
    "weather": "Partly Cloudy",
    "temperature_string": "84 F (29 C)",
    "temp_f": "84",
    "temp_c": "29",
    "relative_humidity": "62%",
    "wind_string": "From the ESE at 7 MPH",
    "wind_dir": "ESE",
    "wind_degrees": "110",
    "wind_mph": "7",
    "pressure_string": "29.83 in (1010 mb)",
    "pressure_mb": "1010",
    "pressure_in": "29.83",
    "dewpoint_string": "70 F (21 C)",
    "dewpoint_f": "70",
    "dewpoint_c": "21",
    "heat_index_string": "88 F (31 C)",
    "heat_index_f": "88",
    "heat_index_c": "31",
    "windchill_string": "NA",
    "windchill_f": "NA",
    "windchill_c": "NA",
    "visibility_mi": "5.0",
    "visibility_km": "8.0",
    "icons": {
      "icon_set": [
        {
          "-name": "Default",
          "icon_url": "http://icons-ak.wxug.com/i/c/a/partlycloudy.gif"
        },
        {
          "-name": "Smiley",
          "icon_url": "http://icons-ak.wxug.com/i/c/b/partlycloudy.gif"
        },
        {
          "-name": "Generic",
          "icon_url": "http://icons-ak.wxug.com/i/c/c/partlycloudy.gif"
        },
        {
          "-name": "Old School",
          "icon_url": "http://icons-ak.wxug.com/i/c/d/partlycloudy.gif"
        },
        {
          "-name": "Cartoon",
          "icon_url": "http://icons-ak.wxug.com/i/c/e/partlycloudy.gif"
        },
        {
          "-name": "Mobile",
          "icon_url": "http://icons-ak.wxug.com/i/c/f/partlycloudy.gif"
        },
        {
          "-name": "Simple",
          "icon_url": "http://icons-ak.wxug.com/i/c/g/partlycloudy.gif"
        },
        {
          "-name": "Contemporary",
          "icon_url": "http://icons-ak.wxug.com/i/c/h/partlycloudy.gif"
        },
        {
          "-name": "Helen",
          "icon_url": "http://icons-ak.wxug.com/i/c/i/partlycloudy.gif"
        },
        {
          "-name": "Incredible",
          "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif"
        }
      ]
    },
    "icon_url_base": "http://icons-ak.wxug.com/graphics/conds/",
    "icon_url_name": ".GIF",
    "icon": "partlycloudy",
    "forecast_url": "
http://www.wunderground.com/global/stations/43278.html
",
    "history_url": "
wunderground.com/history/airport/VOMM/2012/12/11/DailyHistory.html
",
    "ob_url": "
wunderground.com/cgi-bin/findweather/getForecast?query=13.00000000,80.18000031"

  }
      });
4

4 回答 4

2

JSON 是一个对象,您可以使用 JSON.parse() 对其进行解析,也可以遍历节点以获取所需的数据。

例如:current_observation.credit,它将为您提供“Weather Underground NOAA Weather Station”

请尝试发布确切的查询以获得更好的答案。

于 2012-12-12T08:42:24.877 回答
1

如果您尝试将 JavaScript 对象正确插入 HTML(我从您使用 $.getJSON(<-- 此函数仅用于获取“类似字符串”的数组/文字对象中了解到),请尝试使用 JSON。 stringify({dasdas:'fadfds'});

如果您想解析(将字符串解释为 JavaScript 代码中的对象),请按照下面的说明进行操作。

于 2012-12-12T08:49:51.930 回答
0

大多数浏览器都支持 JSON.parse()解析 JSON。由于您使用的是 jQuery,因此您还可以使用jQuery.parseJSON()作为JSON.parse().

于 2012-12-12T08:38:52.840 回答
0

使用JSON.parse()获取对象。此外,如果您有字符串格式的数据,那么您也可以执行 eval()。[http://www.w3schools.com/jsref/jsref_eval.asp]

于 2012-12-12T08:41:49.573 回答