Applescript JSON提取
我有这个JSON文件,我需要从中提取数据:Applescript JSON提取,json,applescript,Json,Applescript,我有这个JSON文件,我需要从中提取数据: { "data1": "Hello", "data2":[ { "value1": "1092", "value2": "1242", "value3": "4234", } ], "data3": "Bye" } 我知道如何使用这段代码从“da
{
"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
将mJson设置为执行shell脚本“curl-s”https://apiURL...'"
将AppleScript的文本项分隔符设置为{“,”}
将keyValueList设置为(mJson中的每个文本项)作为列表
将AppleScript的文本项分隔符设置为“”
将KeyValuePair设置为keyValueList的第3项
将AppleScript的文本项分隔符设置为{”:“}
将KeyValueBufferList设置为(KeyValuePair中的每个文本项)作为列表
将AppleScript的文本项分隔符设置为“”
将该值设置为KeyValueBufferList的第2项
将值设置为执行shell脚本“sed s/[^0-9.”,]//g使用香草AppleScript解析JSON非常烦人
你可以使用帮助程序,或者你可以在基础框架的帮助下使用它。
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助手真的帮了大忙。谢谢vadian