跨域擷取json資料

來源:互聯網
上載者:User

標籤:blog   http   java   io   for   資料   ar   2014   

這是天氣json的資料,這裡是連結,json的資料介面  http://m.weather.com.cn/data/101010100.html

json的資料格式

{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2014年3月4 日","date":"","week":"星期二","fchh":"11","cityid":"101010100","temp1":"8℃~- 3℃","temp2":"8℃~-3℃","temp3":"7℃~-3℃","temp4":"8℃~- 1℃","temp5":"10℃~1℃","temp6":"10℃~2℃","tempF1":"46.4℉~26.6℉","tempF2":"46.4℉~26.6℉","tempF3":"44.6℉~26.6℉","tempF4":"46.4℉~30.2℉","tempF5":"50℉~33.8℉","tempF6":"50℉~35.6℉","weather1":" 晴","weather2":"晴","weather3":"晴","weather4":"晴轉多雲","weather5":"多 雲","weather6":"多 雲","img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"1","img10":"99","img11":"1","img12":"99","img_single":"0","img_title1":" 晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":" 晴","img_title6":"晴","img_title7":"晴","img_title8":"多雲","img_title9":"多 雲","img_title10":"多雲","img_title11":"多雲","img_title12":"多 雲","img_title_single":"晴","wind1":"北風4-5級轉微風","wind2":"微風","wind3":"微 風","wind4":"微風","wind5":"微風","wind6":"微風","fx1":"北風","fx2":"微 風","fl1":"4-5級轉小於3級","fl2":"小於3級","fl3":"小於3級","fl4":"小於3級","fl5":"小於3 級","fl6":"小於3級","index":"寒冷","index_d":"天氣寒冷,建議著厚羽絨服、毛皮大衣加厚毛衣等隆冬服裝。年老體弱者 尤其要注意保暖防凍。","index48":"冷","index48_d":"天氣冷,建議著棉服、羽絨服、皮夾克加羊毛衫等冬季服裝。年老體弱者宜 著厚棉衣、冬大衣或厚羽絨服。","index_uv":"中等","index48_uv":"中等","index_xc":"較適 宜","index_tr":"一般","index_co":"較舒適","st1":"7","st2":"- 3","st3":"8","st4":"0","st5":"7","st6":"-1","index_cl":"較不 宜","index_ls":"基本適宜","index_ag":"易發"}}

這裡是什麼不用說了吧

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Json跨域解決辦法</title>
<script src=‘http://code.jquery.com/jquery-1.8.0.js‘ type=‘text/javascript‘></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/blob/master/json2.js"></script>
</head>
<body>
<div id="content">
</div>
<script>
         $(function () {
             $.getJSON("http://query.yahooapis.com/v1/public/yql", {
                 q: "select * from json where url=\"http://m.weather.com.cn/data/101010100.html\"",
                 format: "json"
             }, function (data) {
                 if (data.query.results) {
                     $("#content").text(JSON.stringify(data.query.results));
                     var J_data = JSON.parse(JSON.stringify(data.query.results));
                     alert(J_data.weatherinfo.city);
 
                 } else {
                     $("#content").text(‘no such code: ‘ + code);
                 }
             });
         });
     </script>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.