1

I am trying to make a weather app for iOS with LiveCode, which it will use a weather API that will provide information in Json format. But how can I realize it?

For example, the api will provide a link

http://m.weather.com.cn/data/101110101.html

(101110101 is the city-code, it can be replaced by different code to gather different weather info)

If you go to the link above, it will provide the weather information of the corresponding city in json format.

How can I gather information and put into the corresponding field? And because the weather api provides different code for different cities, how can I realize the function that I can put a city name in a field then click a button, and the app will gather the weather information?

Here are some city codes you could try, although they are in Chinese :D. The name of city could be in different languages, I just need it to be able to translate the name to its corresponding code.

101010100=北京
101010200=海淀
101010300=朝阳
101010400=顺义
101010500=怀柔
101010600=通州
101010700=昌平
101010800=延庆
101010900=丰台
101011000=石景山
101011100=大兴
101011200=房山
101011300=密云
101011400=门头沟
101011500=平谷
101011600=八达岭
101011700=佛爷顶
101011800=汤河口
101011900=密云上甸子
101012000=斋堂
101012100=霞云岭
Xuanwen Zhang
  • 45
  • 1
  • 6

2 Answers2

1

This is for LiveCode 7 or higher (Unicode text handling is different and less robust in earlier versions.)

Let's say you save your city codes in a utf8 text file, cities.txt. Read in the text file, and convert it to UTF-16, LiveCode's native text encoding. I've stored my text file on the desktop, but obviously you can store it anywhere you want as long as you can derive the path to the file.

On a card with a button, text field "city" and fld "weatherdata", I write the following handler in the button:

on mouseUp
   put the text of fld "city" into tCityName
   put specialFolderPath("desktop") & "/cities.txt" into tFilePath
   put URL ("binfile:" & tFilePath) into tCityList # read file as binary data
   put textDecode(tCityList,"UTF8") into tCityList # convert to UTF16
   put lineOffset("=" & tCityName & cr,tCityList & cr) into tFoundLine
   set the itemDelimiter to "="
   put item 1 of line tFoundLine of tCityList into tCityCode

   # now call the weather API
   put "http://m.weather.com.cn/data/" & tCityCode & ".html" into tURL
   put URL tURL into tRawJSON
   put textDecode(tRawJSON,"UTF8") into fld "weather data"
end mouseUp

Now all that's left is to parse through the JSON. There are several JSON libraries available for LiveCode. But that's a different question.

Devin
  • 593
  • 1
  • 3
  • 8
  • Thank you very much for answering. That helped a lot, but how can I add JSON libraries to LiveCode and use it to parse the my link? I tried what you said and when I clicked the button, tRawJSON appeared in the weatherData field s that what it supposed to look like? – Xuanwen Zhang May 01 '15 at 01:09
  • It's very simple to add a stack library. For example follow Monte's link to EasyJSON in his answer above. Then in your stack use the command: 'library stack /path/to/stackfile.livecode'. That will add all of the handlers in the library stack script to your stack's message path. Once that's done just use the functions in the library as described in the instructions included with the EasyJSON library. – Devin May 02 '15 at 04:55
  • Hi, I did everything but they still appear the same thing. Can you please take a look at it?https://www.dropbox.com/s/9dbttvmbg0e4v9m/WeahterApp.zip?dl=0 – Xuanwen Zhang May 04 '15 at 15:37
0

As Devin mentions there are several JSON libraries available. Indeed if you are experimenting with LC 8 then Peter Brett posted one written in LiveCode Bulder today here.

EasyJSON is written in LC Script and should work on most version of LC. It's available here

I use my external mergJSON which is the fastest JSON parser available for LiveCode at the moment. It's dual licensed and available on GitHub here and as built versions from my website here

In all cases you will want to parse the JSON to a LiveCode array and any text you need to display you will need to textDecode as in Devin's example.

Monte Goulding
  • 2,380
  • 1
  • 21
  • 25