在livecode中提取json数据

时间:2017-09-12 05:42:27

标签: json livecode

我道歉,我是Livecode的新手,我没有编码经验。我试图在livecode中提取wunderground每小时json数据,但我不能

JSON数据:

"response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "hourly": 1
  }
	}
		,
	"hourly_forecast": [
		{
		"FCTTIME": {
		"hour": "12","hour_padded": "12","min": "00","min_unpadded": "0","sec": "0","year": "2017","mon": "9","mon_padded": "09","mon_abbrev": "Sep","mday": "12","mday_padded": "12","yday": "254","isdst": "0","epoch": "1505196000","pretty": "12:00 PM +06 on September 12, 2017","civil": "12:00 PM","month_name": "September","month_name_abbrev": "Sep","weekday_name": "Tuesday","weekday_name_night": "Tuesday Night","weekday_name_abbrev": "Tue","weekday_name_unlang": "Tuesday","weekday_name_night_unlang": "Tuesday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
		},
		"temp": {"english": "85", "metric": "29"},
		"dewpoint": {"english": "77", "metric": "25"},
		"condition": "Thunderstorm",
		"icon": "tstorms",
		"icon_url":"http://icons.wxug.com/i/c/k/tstorms.gif",
		"fctcode": "15",
		"sky": "75",
		"wspd": {"english": "4", "metric": "6"},
		"wdir": {"dir": "S", "degrees": "185"},
		"wx": "Thunderstorms",
		"uvi": "11",
		"humidity": "77",
		"windchill": {"english": "-9999", "metric": "-9999"},
		"heatindex": {"english": "94", "metric": "35"},
		"feelslike": {"english": "94", "metric": "35"},
		"qpf": {"english": "0.03", "metric": "1"},
		"snow": {"english": "0.0", "metric": "0"},
		"pop": "61",
		"mslp": {"english": "29.78", "metric": "1008"}
		}
		,

我将EasyJSON https://github.com/luxlogica/easyjson复制到我的StackScript中。我的按钮代码如下所示:

on mouseUp
   put the text of fld "city" into tCityName
   put the text of fld "country" into tCountry
   
   
put "http://api.wunderground.com/api/dfabb014e63a7457/hourly/q/" &tCountry&"/"&tCityName& ".json" into tURL
put URL tURL into tRawJSON
put textDecode(tRawJSON,"UTF8") into fld "weatherdata"
put arrayFromJson(tRawJSON) into tArray

put tArray["FCTTIME"]["temp"] into fld "Temp"

end mouseUp

2 个答案:

答案 0 :(得分:0)

在版本8及更高版本中,LiveCode中包含一个JSON库,其中包含两个函数:JsonImport和JsonExport。 JsonImport将JSON编码数据转换为数组。

put the text of fld "city" into tCityName
put the text of fld "country" into tCountry
put "http://api.wunderground.com/api/dfabb014e63a7457/hourly/q/" &tCountry&"/"&tCityName& ".json" into tURL
put URL tURL into tRawJSON
put textDecode(tRawJSON,"UTF8") into fld "weatherdata"
put JsonImport(fld "weatherdata") into tArray
put the keys of tArray into fld "Temp"

在v.8及更高版本中还有一个方便的树视图小部件,可以很容易地显示数组的内容。只需将树视图小部件从工具选项板拖到卡片上即可:

set the arrayData of widget "tree view" to tArray

答案 1 :(得分:0)

感谢德文的回复。 我得到了解决方案:



put tArray["hourly_forecast"][1]["temp"]["metric"] into fld "Temp"