## **PHP获取数据** ``` <?php header("Content-Type: text/html; charset=utf-8"); $data = file_get_contents('http://api.tianapi.com/wxnew/?key=APIKEY'); //请求API $json = json_decode($data,true); //将json解析成数组 if($json['code'] == 200){ //判断状态码 $txapi=$json['newslist']; for ($i = 0; $i < count($txapi); $i++){ //循环遍历数组 $title = $txapi[$i]['title']; //文章标题 $desc = $txapi[$i]['description']; //文章描述 $pic = $txapi[$i]['picUrl']; //封面图片 $url = $txapi[$i]['url']; //文章链接 echo $title."<br/>".$desc."<br/>".$pic."<br/>".$url."<br/>"; //输出内容 } }else{ echo "返回错误,状态消息:".$json['msg']; } ?> ``` ## **python获取数据** ~~~ # -*- coding: utf-8 -*- import sys, urllib, urllib2, json url = 'http://api.tianapi.com/wxnew/?key=您的key&num=10' req = urllib2.Request(url) resp = urllib2.urlopen(req) content = resp.read() if(content): print(content) ~~~ ## **JAVA获取数据** ~~~ String httpUrl = "http://api.tianapi.com/wxnew/?key=您的key&num"; String jsonResult = request(httpUrl); System.out.println(jsonResult); /** * @param urlAll * :请求接口 * @param httpArg * :参数 * @return 返回结果 */ public static String request(String httpUrl, String httpArg) { BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?" + httpArg; try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } ~~~ ## **AJAX获取数据** ~~~ $(document).ready(function(){ $('#button').click(function(){ $.ajax({ type:"GET", url:"http://api.tianapi.com/wxnew/?key=您的key&num=10", dataType:"json", success:function(data){ var txapi="<ul>"; for(var i=0; i<10; i++) { txapi+="<li>"+data["newslist"][i]["title"]+"</li>"; txapi+="<li>"+data["newslist"][i]["description"]+"</li>"; txapi+="<li>"+data["newslist"][i]["picUrl"]+"</li>"; txapi+="<li>"+data["newslist"][i]["url"]+"</li>"; } txapi+="</ul>"; $('#result').append(txapi); } }); return false; }); }); ~~~