无法从Applescript中的JSON获取数据

时间:2018-02-06 02:59:29

标签: json applescript

所以我能够从一个api获取值,但我无法获得第二个值,即货币转换率(来自完全不同的来源)。我在下面粘贴了我的代码,非常感谢任何帮助。

set json to (do shell script "curl https://www.bitstamp.net/api/v2/ticker/xrpusd")

tell application "JSON Helper"
    set result to read JSON from json
    set price to |last| of result as number
end tell

set json to (do shell script "curl https://api.fixer.io/latest?symbols=INR")

tell application "JSON Helper"
    set result to read JSON from json
    set inr to |INR| of result as number
end tell

set result_string to "₹" & price * inr & ""

1 个答案:

答案 0 :(得分:0)

如果我在浏览器中打开第二个URL,这里是返回的JSON(格式化以便于阅读):

{"base":"EUR",
 "date":"2018-02-05",
 "rates":{
     "INR":79.744
     }
}

INR值位于rates值内。因此,您需要像这样引用它:

    set inr to (INR of rates of result) as number

我认为没有任何理由将INR括在竖条中,因为没有冲突的AppleScript命令或具有该名称的变量。您甚至可以将inr定义为用户定义的变量,AppleScript将能够区分您引用INR of rates时的引用时间。

但是,result是一个预定义的AppleScript属性,所以我会避免以你拥有的方式使用它。你能做的就是改变这一行:

    set result to read JSON from json 

到此:

    read JSON from json

并保持后续行不变(我从上面建议的修正)。现在,您将正确使用AppleScript result属性。

相关问题