Applescript JSON 提取
Applescript JSON extraction
我有这个 JSON 文件,我需要从中提取数据:
{
"data1": "Hello",
"data2":[
{
"value1": "1092",
"value2": "1242",
"value3": "4234",
}
],
"data3": "Bye"
}
我知道如何使用这段代码从 "data1"
和 "data3"
中获取值
set mJson to do shell script "curl -s 'https://apiURL...'"
set AppleScript's text item delimiters to {","}
set keyValueList to (every text item in mJson) as list
set AppleScript's text item delimiters to ""
set theKeyValuePair to item 3 of keyValueList
set AppleScript's text item delimiters to {": "}
set theKeyValueBufferList to (every text item in theKeyValuePair) as list
set AppleScript's text item delimiters to ""
set theValue to item 2 of theKeyValueBufferList
set value to do shell script "sed s/[^0-9.,]//g <<< " & theValue
return "$" & text 1 thru 7 of value
但是如何获取 "data2"
> "value1"
中的值,因为它在 JSON 文件的 "sub categorie" 中?
有人可以帮我吗?
使用 vanilla AppleScript 解析 JSON 非常烦人。
您可以使用 JSON Helper 之类的辅助应用程序,也可以借助 Foundation 框架
set json to "{\"data1\": \"Hello\",\"data2\":[{\"value1\": \"1092\",\"value2\": \"1242\",\"value3\": \"4234\",
}],\"data3\": \"Bye\"}"
set foundationString to current application's NSString's stringWithString:json
set jsonData to foundationString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {dataDictionary, jsonError} to current application's NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(reference)
if jsonError is missing value then
set data1 to (dataDictionary's objectForKey:"data1") as text
set data2 to (dataDictionary's objectForKey:"data2")'s objectAtIndex:0
set value1 to (data2's objectForKey:"value1") as text
end if
我有这个 JSON 文件,我需要从中提取数据:
{
"data1": "Hello",
"data2":[
{
"value1": "1092",
"value2": "1242",
"value3": "4234",
}
],
"data3": "Bye"
}
我知道如何使用这段代码从 "data1"
和 "data3"
中获取值
set mJson to do shell script "curl -s 'https://apiURL...'"
set AppleScript's text item delimiters to {","}
set keyValueList to (every text item in mJson) as list
set AppleScript's text item delimiters to ""
set theKeyValuePair to item 3 of keyValueList
set AppleScript's text item delimiters to {": "}
set theKeyValueBufferList to (every text item in theKeyValuePair) as list
set AppleScript's text item delimiters to ""
set theValue to item 2 of theKeyValueBufferList
set value to do shell script "sed s/[^0-9.,]//g <<< " & theValue
return "$" & text 1 thru 7 of value
但是如何获取 "data2"
> "value1"
中的值,因为它在 JSON 文件的 "sub categorie" 中?
有人可以帮我吗?
使用 vanilla AppleScript 解析 JSON 非常烦人。
您可以使用 JSON Helper 之类的辅助应用程序,也可以借助 Foundation 框架
set json to "{\"data1\": \"Hello\",\"data2\":[{\"value1\": \"1092\",\"value2\": \"1242\",\"value3\": \"4234\",
}],\"data3\": \"Bye\"}"
set foundationString to current application's NSString's stringWithString:json
set jsonData to foundationString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
set {dataDictionary, jsonError} to current application's NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(reference)
if jsonError is missing value then
set data1 to (dataDictionary's objectForKey:"data1") as text
set data2 to (dataDictionary's objectForKey:"data2")'s objectAtIndex:0
set value1 to (data2's objectForKey:"value1") as text
end if