Xuanwen Zhang
Xuanwen Zhang

Reputation: 45

How to use API and decoding json in livecode

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=霞云岭

Upvotes: 1

Views: 1505

Answers (2)

Monte Goulding
Monte Goulding

Reputation: 2420

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.

Upvotes: 0

Devin
Devin

Reputation: 603

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.

Upvotes: 1

Related Questions