【发布时间】:2016-06-10 22:37:10
【问题描述】:
我找到了这个 Q&A (Can and how do you get remote data (e.g. JSON) into AppleScript?),并开始使用 JSON Helper。
JSON 数据
{
"prop1": "text",
"prop2": [
{
"prop3": "text",
"prop4": "text"
}
]
}
示例程序代码
-- set configPath to path to resource "config.json"
set configPath to POSIX path of (path to resource "config.json")
-- set configPath to "file://" & POSIX path of (path to resource "config.json")
set configOptionsString to (read POSIX file configPath)
tell application "JSON Helper"
-- set configOptions to fetch JSON from configPath
set configOptions to read JSON from configOptionsString
-- set configOptions to make JSON from configOptionsString
end tell
-- display dialog configPath as string
display dialog configOptionsString
display dialog configOptions
display dialog configOptions as string
结果
configPath 正确显示基于 '/' 的 POSIX 路径。
configOptionsString 正确包含 JSON 文本。
configOptions 错误地为空。
方法#1
唯一的问题是,在尝试使用 fetch JSON from 命令时,我无法强制它使用本地文件路径并获取我期望的文本/JSON 内容。
我已经尝试过 HFS 和 POSIX 样式的文件路径,包括带有 file:// 前缀的 POSIX,导致这种格式 (file:///...)。我还通过将as string 附加到命令来解决任何别名错误(我认为是HFS 路径),例如在通过display dialog 指令调试查看路径时。
我知道它可以找到我正在使用的资源:@987654334@。在我重新启动脚本编辑器之前,它有一个关于找不到资源的错误。
方法#2
我什至尝试正常加载文件的内容,并使用read JSON from 命令将它们解析为字符串:
set configOptionsString to (read POSIX file configPath)
tell application "JSON Helper"
set configOptions to read JSON from configOptionsString
end tell
方法#3
为了完整起见,我尝试使用相同的字符串并通过make JSON from 命令运行它,只是为了尝试它,但它也会产生空输出。
问题
JSON Helper (Examples) 是否能够做到这一点,如果有,我应该如何,或者我应该尝试使用诸如 applescript-json 之类的工具来代替吗?
【问题讨论】:
标签: json applescript