2017-02-11 64 views
0

我完全不知道我在说什么,这是一个大问题。AppleScript grap来自safari的JSON数据

我经常遇到一个内部网站上进行搜索,让说

https://theexemplewebsite.demo/

我发现,这种模式搜索(XXXXXXX是我的搜索关键字),我可以检索的信息,我想要的东西看起来像JSON(我想...)

https://theexemplewebsite.demo/admin/api/v1/profil?id=XXXXXXX

如:

{"aDate":11111,"bDate":111112,,"OtherData":false,"example":"D01","moreData":"DEMO","description":"OSX computer",等等...

有没有办法得到这个信息,例如OSX计算机从“说明”:“OSX的电脑”,并将其存储在一个变量的AppleScript ?

我希望这是有道理的。 谢谢。

回答

1

如果你得到一个有效的JSON字符串你可以使用AppleScriptObjC - 它提供了Cocoa类访问像NSJSONSerialization - 解析它:

use framework "Foundation" 

set jsonString to "{\"aDate\":11111,\"bDate\":111112,\"OtherData\":false,\"example\":\"D01\",\"moreData\":\"DEMO\",\"description\":\"OSX computer\"}" 

set jsonNSString to current application's NSString's stringWithString:jsonString 
set jsonData to jsonNSString's dataUsingEncoding:(current application's NSUTF8StringEncoding) 
set {jsonDict, theError} to current application's NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(reference) 
if theError is missing value then 
    set theDescription to jsonDict's |description| as text 
    display dialog theDescription 
else 
    display dialog theError 
end if