你可以使用AppleScript的do shell script
命令结合jq
工具来提取JSON数据。下面是一个示例代码:
set jsonString to "{
\"name\": \"John\",
\"age\": 30,
\"city\": \"New York\",
\"pets\": [
{
\"name\": \"Max\",
\"type\": \"dog\"
},
{
\"name\": \"Lucy\",
\"type\": \"cat\"
}
]
}"
-- 使用jq命令提取JSON数据
set extractedData to do shell script "echo " & quoted form of jsonString & " | jq '.name, .age, .city'"
-- 输出提取的数据
display dialog extractedData
上述代码中,我们首先定义了一个包含JSON数据的字符串jsonString
。然后,使用do shell script
命令将字符串传递给jq
命令,使用.name, .age, .city
参数来提取JSON中的name
、age
和city
字段的值。最后,将提取的数据显示在一个对话框中。
请确保你的系统中已经安装了jq
工具,你可以在终端中运行jq
命令来检查。如果没有安装,你可以通过Homebrew等工具安装。
上一篇:AppleScript 和 Numbers:无法给单元格添加值
下一篇:Applescript Mail - 脚本无法通过“on perform mail action with messages theMessages for rule theRule”