当前位置:编程学习 > JS >>

iOS学习笔记29—天气JSON接口

想写个天气的应用,在网上找了一圈都没找到有关 JSONP 的接口,只有中国天气网的 JSON 接口,这就简单,直接用 SAE 做代理,将数据处理为 JSONP 的形式返回就行了,顺便分析了中国天气网的省市区三级联动的数据,这样就直接可以做天气应用了。


说说我的接口的用法吧,直接 get 请求,
参数有三个 。

get 参数,获取操作
可以取值 "province","city","town","weather","weathern";分别是获取省 (直辖市) 的列表,根据省 ID 获取市的列表,根据省市的 ID 获取区县列表,根据省市区 ID 获取天气预报,根据省市区 ID 获取实时天气。
citycode 参数
就是操作中需要的 ID,获取省列表就可以不需要了
callback 参数
就是 JSONP 的 callback 啦。
具体用法看看我的 Demo 吧,demo 地址:天气接口 JSONP 示例,

 

 [javascript]
<SPAN style="FONT-SIZE: 14px"><SPAN style="COLOR: rgb(0,0,102)"><STRONG>var</STRONG></SPAN> baseUrl <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #3366cc">"http://demo.alphatr.com/weather/info.php"</SPAN><SPAN style="COLOR: #339933">;</SPAN> 
  
<SPAN style="COLOR: #006600">// 将获取的省市区列表 JSON 格式转换为 HTML 字符串</SPAN>  
<SPAN style="COLOR: #000066"><STRONG>var</STRONG></SPAN> appendData <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #000066"><STRONG>function</STRONG></SPAN><SPAN style="COLOR: #009900">(</SPAN> data <SPAN style="COLOR: #339933">,</SPAN> el <SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN> 
    <SPAN style="COLOR: #000066"><STRONG>var</STRONG></SPAN> list <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #009900">[</SPAN><SPAN style="COLOR: #009900">]</SPAN><SPAN style="COLOR: #339933">;</SPAN> 
    $.<SPAN style="COLOR: #660066">each</SPAN><SPAN style="COLOR: #009900">(</SPAN>data<SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #000066"><STRONG>function</STRONG></SPAN><SPAN style="COLOR: #009900">(</SPAN> key<SPAN style="COLOR: #339933">,</SPAN> val <SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #009900">{</SPAN> 
        <SPAN style="COLOR: #006600">// console.log(key);</SPAN>  
        list.<SPAN style="COLOR: #660066">push</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #3366cc">'<option value="'</SPAN> <SPAN style="COLOR: #339933">+</SPAN> key <SPAN style="COLOR: #339933">+</SPAN> <SPAN style="COLOR: #3366cc">'">'</SPAN> <SPAN style="COLOR: #339933">+</SPAN> val <SPAN style="COLOR: #339933">+</SPAN> <SPAN style="COLOR: #3366cc">'</option>'</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN> 
    <SPAN style="COLOR: #009900">}</SPAN><SPAN style="COLOR: #009900">)</SPAN> 
    $<SPAN style="COLOR: #009900">(</SPAN>el<SPAN style="COLOR: #009900">)</SPAN>.<SPAN style="COLOR: #660066">html</SPAN><SPAN style="COLOR: #009900">(</SPAN>list.<SPAN style="COLOR: #660066">join</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #3366cc">''</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN> 
<SPAN style="COLOR: #009900">}</SPAN> 
<SPAN style="COLOR: #006600">/* 三级联动 */</SPAN> 
<SPAN style="COLOR: #006600">// 获取省列表</SPAN>  
$.<SPAN style="COLOR: #660066">getJSON</SPAN><SPAN style="COLOR: #009900">(</SPAN> baseUrl <SPAN style="COLOR: #339933">+</SPAN> <SPAN style="COLOR: #3366cc">"?get=province&callback=?"</SPAN> <SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #000066"><STRONG>function</STRONG></SPAN> <SPAN style="COLOR: #009900">(</SPAN>data<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN> 
    appendData<SPAN style="COLOR: #009900">(</SPAN> data <SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #3366cc">"#province"</SPAN> <SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN> 
    <SPAN style="COLOR: #000066"><STRONG>var</STRONG></SPAN> provCode <SPAN style="COLOR: #339933">=</SPAN> $<SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #3366cc">"#province"</SPAN><SPAN style="COLOR: #009900">)</SPAN>.<SPAN style="COLOR: #660066">val</SPAN><SPAN style="COLOR: #009900">(</SPAN><SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN> <SPAN style="COLOR: #006600">// 省 ID</SPAN>  
    <SPAN style="COLOR: #006600">// 根据省 ID 获取市列表</SPAN>  
    $.<SPAN style="COLOR: #660066">getJSON</SPAN><SPAN style="COLOR: #009900">(</SPAN> baseUrl <SPAN style="COLOR: #339933">+</SPAN> <SPAN style="COLOR: #3366cc">"?get=city&citycode="</SPAN> <SPAN style="COLOR: #339933">+</SPAN> provCode <SPAN style="COLOR: #339933">+</SPAN> <SPAN style="COLOR: #3366cc">"&callback=?"</SPAN> <SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #000066"><STRONG>function</STRONG></SPAN> <SPAN style="COLOR: #009900">(</SPAN>data<SPAN style="COLOR: #009900">)</SPAN> <SPAN style="COLOR: #009900">{</SPAN> 
        appendData<SPAN style="COLOR: #009900">(</SPAN> data <SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #3366cc">"#city"</SPAN> <SPAN style="COLOR: #009900">)</SPAN><SPAN style="COLOR: #339933">;</SPAN> 
        <SPAN style="COLOR: #000066"><STRONG>var</STRONG></

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,