2017-02-11 66 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