I have three elements on my stack, "weatherdata" field, "city"field and a button. The idea is there is a "cities.txt"file saved in the desktop stored the city name with its corresponding code that is identical to the API. Something like this:
101010100=Beijing
101010300=Chaoyang
101010400=Shuangyi
101010500=Huairou
101010600=Tongzhou
101010700=Changping
101010800=Yanqing
the API is a link: http://m.weather.com.cn/data/ (city code.html) For example if I want the weather info of Beijng the link would be look like:http://m.weather.com.cn/data/101110101.html
when get into the link, it will provide the weather data in JSON format like:
Code: Select all
{"weatherinfo":{"city":"西安","city_en":"xian","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101110101","temp1":"13℃~3℃"
Basically, what I want is when I type Beijing in city field and click the button, the app will replace the (city code.html)part to corresponding city code form "cities.txt". And then the "weatherdata" field will display the information provided by the API in a format that we could read.
So far I copied EasyJSON https://github.com/luxlogica/easyjson into my StackScript. And my button code looks like this:
Code: Select all
on mouseUp
put the text of fld "city" into tCityName
put specialFolderPath("desktop") & "/cities.txt" into tFilePath
put textDecode(tCityList,"UTF8") into tCityList
put lineOffset("=" & tCityName & cr,tCityList & cr) into tFoundLine
set the itemDelimiter to "="
put item 1 of line tFoundLine of tCityList into tCityCode
put "http://m.weather.com.cn/data/" & tCityCode & ".html" into tURL
put URL tURL into tRawJSON
put textDecode(tRawJSON,"UTF8") into fld "weatherdata"
end mouseUp
Code: Select all
<html>
<head>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
window.open("/","_self");
};
</script>
<!-- START WRating v1.0 -->
<script type="text/javascript" src="http://c.wrating.com/a1.js">
</script>
<script type="text/javascript">
var vjAcc="860010-2151010100";
var wrUrl="http://c.wrating.com/";
vjTrack("");
</script>
<noscript><img src="http://c.wrating.com/a.gif?a=&c=860010-2151010100" width="1" height="1"/></noscript>
<!-- END WRating v1.0 -->
</body>
</html>