【问题标题】:extract json data in livecode在 livecode 中提取 json 数据
【发布时间】:2017-09-12 05:42:27
【问题描述】:

抱歉,我是 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

【问题讨论】:

  • 你没有说你的代码会发生什么或结果是什么。

标签: json livecode


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    在版本 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"
    

    第 8 版及更高版本中还有一个方便的树视图小部件,可以轻松可视化数组的内容。只需将树视图小部件从工具面板拖到卡片上,然后执行以下操作:

    set the arrayData of widget "tree view" to tArray
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 2021-06-13
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多