从 AppleScript 解析文本
parsing a text from AppleScript
我试图从我在 AppleScript 中使用
运行 的脚本的响应中仅获取一种声音
do shell script "<mycommand>"
基本上我只需要将这个"Ho1u8aA2p8jGuzEIZq8n"
存储为变量以备后用。
下面是我收到的完整字符串。
我认为 awk
应该是解决方案,但不幸的是我运气不好。
知道如何实现吗?
下面是我收到的完整字符串,它是来自在 firebase 中创建的新条目的报告。
{"id":"Ho1u8aA2p8jGuzEIZq8n","data":{"spa_id":"0","os":"10.14.6","name":"Spa_Name","activity":"0","spa_email":"info@spa.com","serial":"C07XMRXWJYVW","nation":"Italy","spa_address":"Apliu Street 54, Hong Kong","ip_address":"192.168.2.1","global_unit_id":"0","city":"Perugia","branch":"Main","spa_unit_nr":"1","uid":"dev","spa_website":"www.spaname.com","continent":"Europe","spa_phone":"+852 3847569"}}
这可以通过管道传递给 cut
命令,用双引号作为字段分隔符。
script | cut -d'"' -f3
如果任何 JSON 值或键包含引号。 "
,然后用它作为分隔符用cut
之类的工具分割文本是行不通的。同样,逗号——这是另一个很有吸引力的字符,可以被视为潜在的分隔符——也存在同样的问题。您还会丢失键值关联,这可能是一个障碍。
下面的脚本应该提供更全面的解决方案,适用于更一般的情况,因为它专门针对 JSON 字符串,并将它们转换为本机 AppleScript 数据类型(record
), 因此键值关系仍然存在(尽管是单边的)。
set JSON to JSONStringToRecord from "{
\"id\": \"Ho1u8aA2p8jGuzEIZq8n\",
\"data\": {
\"spa_id\": \"0\",
\"os\": \"10.14.6\",
\"name\": \"Spa_Name\",
\"activity\": \"0\",
\"spa_email\": \"info@spa.com\",
\"serial\": \"C07XMRXWJYVW\",
\"nation\": \"Italy\",
\"spa_address\": \"Apliu Street 54, Hong Kong\",
\"ip_address\": \"192.168.2.1\",
\"global_unit_id\": \"0\",
\"city\": \"Perugia\",
\"branch\": \"Main\",
\"spa_unit_nr\": \"1\",
\"uid\": \"dev\",
\"spa_website\": \"www.spaname.com\",
\"continent\": \"Europe\",
\"spa_phone\": \"+852 3847569\"
}
}"
on JSONStringToRecord from input as text
local input
script objc
use framework "Foundation"
to unwrap()
tell the current application to tell every reference in ¬
(NSJSONSerialization's JSONObjectWithData:((NSString's ¬
stringWithString:input)'s dataUsingEncoding:4) ¬
options:0 |error|:(reference)) to if it ≠ {} ¬
then return its item 1 as {record, list, text}
end unwrap()
end script
unwrap() in objc
end JSONStringToRecord
我获取了您的 JSON 字符串输出并对其进行了格式化以便于阅读。但是在你的情况下,上面脚本中的变量 JSON
看起来更像是定义:
set JSONString to do shell script "<mycommand>"
set JSON to JSONStringToRecord from the JSONString
那应该return记录如下:
{
|id|:"Ho1u8aA2p8jGuzEIZq8n",
|data|:{
spa_id:"0",
os: "10.14.6",
|name|: "Spa_Name",
activity: "0",
spa_email: "info@spa.com",
serial: "C07XMRXWJYVW",
nation: "Italy",
spa_address: "Apliu Street 54, Hong Kong",
ip_address: "192.168.2.1",
global_unit_id: "0",
city: "Perugia",
branch: "Main",
spa_unit_nr: "1",
uid: "dev",
spa_website: "www.spaname.com",
continent: "Europe",
spa_phone: "+852 3847569"
}
}
为了便于阅读而格式化,在 脚本编辑器 中看起来会更小,即:
{|id|:"Ho1u8aA2p8jGuzEIZq8n",|data|:{spa_id:"0",os:"10.14.6",|name|:"Spa_Name",activity:"0",spa_email:"info@spa.com",serial:"C07XMRXWJYVW",nation:"Italy",spa_address:"Apliu Street 54, Hong Kong",ip_address:"192.168.2.1",global_unit_id:"0",city:"Perugia",branch:"Main",spa_unit_nr:"1",uid:"dev",spa_website:"www.spaname.com",continent:"Europe",spa_phone:"+852 3847569"}}
这显然与您开始使用的 JSON 字符串非常相似,但在 脚本编辑器 中会更清楚,它不是文本,而是一个本机数据对象,现在可以在 AppleScript 中访问键,以隔离所需的 JSON 记录中的值。因此:
|id| of JSON
会给你 "Ho1u8aA2p8jGuzEIZq8n"
。要获得嵌套较多的项目之一,您可以这样做:
ip_address of |data| of JSON
这会让你 "192.168.2.1"
.
我试图从我在 AppleScript 中使用
运行 的脚本的响应中仅获取一种声音do shell script "<mycommand>"
基本上我只需要将这个"Ho1u8aA2p8jGuzEIZq8n"
存储为变量以备后用。
下面是我收到的完整字符串。
我认为 awk
应该是解决方案,但不幸的是我运气不好。
知道如何实现吗?
下面是我收到的完整字符串,它是来自在 firebase 中创建的新条目的报告。
{"id":"Ho1u8aA2p8jGuzEIZq8n","data":{"spa_id":"0","os":"10.14.6","name":"Spa_Name","activity":"0","spa_email":"info@spa.com","serial":"C07XMRXWJYVW","nation":"Italy","spa_address":"Apliu Street 54, Hong Kong","ip_address":"192.168.2.1","global_unit_id":"0","city":"Perugia","branch":"Main","spa_unit_nr":"1","uid":"dev","spa_website":"www.spaname.com","continent":"Europe","spa_phone":"+852 3847569"}}
这可以通过管道传递给 cut
命令,用双引号作为字段分隔符。
script | cut -d'"' -f3
如果任何 JSON 值或键包含引号。 "
,然后用它作为分隔符用cut
之类的工具分割文本是行不通的。同样,逗号——这是另一个很有吸引力的字符,可以被视为潜在的分隔符——也存在同样的问题。您还会丢失键值关联,这可能是一个障碍。
下面的脚本应该提供更全面的解决方案,适用于更一般的情况,因为它专门针对 JSON 字符串,并将它们转换为本机 AppleScript 数据类型(record
), 因此键值关系仍然存在(尽管是单边的)。
set JSON to JSONStringToRecord from "{
\"id\": \"Ho1u8aA2p8jGuzEIZq8n\",
\"data\": {
\"spa_id\": \"0\",
\"os\": \"10.14.6\",
\"name\": \"Spa_Name\",
\"activity\": \"0\",
\"spa_email\": \"info@spa.com\",
\"serial\": \"C07XMRXWJYVW\",
\"nation\": \"Italy\",
\"spa_address\": \"Apliu Street 54, Hong Kong\",
\"ip_address\": \"192.168.2.1\",
\"global_unit_id\": \"0\",
\"city\": \"Perugia\",
\"branch\": \"Main\",
\"spa_unit_nr\": \"1\",
\"uid\": \"dev\",
\"spa_website\": \"www.spaname.com\",
\"continent\": \"Europe\",
\"spa_phone\": \"+852 3847569\"
}
}"
on JSONStringToRecord from input as text
local input
script objc
use framework "Foundation"
to unwrap()
tell the current application to tell every reference in ¬
(NSJSONSerialization's JSONObjectWithData:((NSString's ¬
stringWithString:input)'s dataUsingEncoding:4) ¬
options:0 |error|:(reference)) to if it ≠ {} ¬
then return its item 1 as {record, list, text}
end unwrap()
end script
unwrap() in objc
end JSONStringToRecord
我获取了您的 JSON 字符串输出并对其进行了格式化以便于阅读。但是在你的情况下,上面脚本中的变量 JSON
看起来更像是定义:
set JSONString to do shell script "<mycommand>"
set JSON to JSONStringToRecord from the JSONString
那应该return记录如下:
{
|id|:"Ho1u8aA2p8jGuzEIZq8n",
|data|:{
spa_id:"0",
os: "10.14.6",
|name|: "Spa_Name",
activity: "0",
spa_email: "info@spa.com",
serial: "C07XMRXWJYVW",
nation: "Italy",
spa_address: "Apliu Street 54, Hong Kong",
ip_address: "192.168.2.1",
global_unit_id: "0",
city: "Perugia",
branch: "Main",
spa_unit_nr: "1",
uid: "dev",
spa_website: "www.spaname.com",
continent: "Europe",
spa_phone: "+852 3847569"
}
}
为了便于阅读而格式化,在 脚本编辑器 中看起来会更小,即:
{|id|:"Ho1u8aA2p8jGuzEIZq8n",|data|:{spa_id:"0",os:"10.14.6",|name|:"Spa_Name",activity:"0",spa_email:"info@spa.com",serial:"C07XMRXWJYVW",nation:"Italy",spa_address:"Apliu Street 54, Hong Kong",ip_address:"192.168.2.1",global_unit_id:"0",city:"Perugia",branch:"Main",spa_unit_nr:"1",uid:"dev",spa_website:"www.spaname.com",continent:"Europe",spa_phone:"+852 3847569"}}
这显然与您开始使用的 JSON 字符串非常相似,但在 脚本编辑器 中会更清楚,它不是文本,而是一个本机数据对象,现在可以在 AppleScript 中访问键,以隔离所需的 JSON 记录中的值。因此:
|id| of JSON
会给你 "Ho1u8aA2p8jGuzEIZq8n"
。要获得嵌套较多的项目之一,您可以这样做:
ip_address of |data| of JSON
这会让你 "192.168.2.1"
.