Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在livecode中使用API和解码json_Json_Livecode - Fatal编程技术网

如何在livecode中使用API和解码json

如何在livecode中使用API和解码json,json,livecode,Json,Livecode,我正在尝试用LiveCode为iOS制作一个天气应用程序,它将使用一个天气API,以Json格式提供信息。但是我怎么能实现呢 例如,api将提供一个链接 (101110101是城市代码,可以用不同的代码替换,以收集不同的天气信息) 如果您转到上面的链接,它将以json格式提供相应城市的天气信息 我如何收集信息并输入相应字段?由于天气api为不同的城市提供了不同的代码,我如何实现在字段中输入城市名称,然后单击按钮,应用程序将收集天气信息的功能 这里有一些你可以尝试的城市代码,虽然它们是中文的:D。

我正在尝试用LiveCode为iOS制作一个天气应用程序,它将使用一个天气API,以Json格式提供信息。但是我怎么能实现呢

例如,api将提供一个链接

(101110101是城市代码,可以用不同的代码替换,以收集不同的天气信息)

如果您转到上面的链接,它将以json格式提供相应城市的天气信息

我如何收集信息并输入相应字段?由于天气api为不同的城市提供了不同的代码,我如何实现在字段中输入城市名称,然后单击按钮,应用程序将收集天气信息的功能

这里有一些你可以尝试的城市代码,虽然它们是中文的:D。城市的名称可以用不同的语言,我只需要它能够将名称翻译成相应的代码


这适用于LiveCode 7或更高版本(Unicode文本处理不同,在早期版本中不太可靠。)

假设您将城市代码保存在utf8文本文件cities.txt中。读入文本文件,并将其转换为UTF-16,即LiveCode的本机文本编码。我已经在桌面上存储了我的文本文件,但是很明显,只要可以导出文件的路径,您就可以将其存储在任何您想要的地方

在带有按钮、文本字段“city”和fld“weatherdata”的卡片上,我在按钮中写入以下处理程序:

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

现在只剩下通过JSON进行解析了。有几个JSON库可用于LiveCode。但这是另一个问题。

正如Devin提到的,有几个JSON库可用。事实上,如果您正在试验LC 8,那么Peter Brett今天发布了一篇用LiveCode Bulder编写的文章

EasyJSON是用LC脚本编写的,应该适用于大多数LC版本。有空

我使用我的外部mergJSON,它是目前LiveCode可用的最快的JSON解析器。它是双重许可的,可以在GitHub上获得,也可以从我的网站上获得


在所有情况下,您都需要将JSON解析为一个LiveCode数组,需要显示的任何文本都需要进行文本解码,如Devin的示例所示。

非常感谢您的回答。这帮助很大,但是我如何才能将JSON库添加到LiveCode中,并使用它来解析my链接?我尝试了你所说的,当我点击按钮时,tRawJSON出现在weatherData字段中,这就是它应该是什么样子?添加堆栈库非常简单。例如,请遵循Monte在上面的答案中指向EasyJSON的链接。然后在堆栈中使用命令:“library stack/path/to/stackfile.livecode”。这将把库堆栈脚本中的所有处理程序添加到堆栈的消息路径中。完成后,只需按照EasyJSON库附带的说明使用库中的函数即可。您好,我做了所有操作,但它们看起来仍然一样。你能看一下吗?
101010100=北京
101010200=海淀
101010300=朝阳
101010400=顺义
101010500=怀柔
101010600=通州
101010700=昌平
101010800=延庆
101010900=丰台
101011000=石景山
101011100=大兴
101011200=房山
101011300=密云
101011400=门头沟
101011500=平谷
101011600=八达岭
101011700=佛爷顶
101011800=汤河口
101011900=密云上甸子
101012000=斋堂
101012100=霞云岭
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