在AppleScript中访问列表中的项目?

时间:2018-02-11 17:48:55

标签: json api applescript

我试图通过AppleScript访问列表中的项目。尽我所能,我似乎无法访问它们。我已将我的代码粘贴在

下面
  

告诉应用程序" JSON Helper"       设置结果以从" https://newsapi.org/v2/top-headlines?sources=bbc-news&pageSize=1&apiKey=X"

获取JSON      

将新闻设置为结果文章的标题

     

告诉

     

将result_string设置为news& ""

请注意,我已删除了我的api密钥。

api格式为:

  

{"状态":" OK""使用totalResults":10,"物品":[{"源&# 34;:{" id":" bbc-news"," name":" BBC News"}," author&# 34;:" BBC新闻","标题":"特朗普敦促以色列人关心'关于定居点","描述":"美国总统也怀疑巴勒斯坦人或以色列是否准备好谈和平。"," url" :" http://www.bbc.co.uk/news/world-middle-east-43025705"" urlToImage":" https://ichef.bbci.co.uk/news/1024/branded_news/165CD/production/_99979519_044058382.jpg"" publishedAt":&#34 ; 2018-02-11T16:47:27Z"}]}

我试图访问标题,但我不断获得#34;无法获得标题"。

非常感谢任何输入。谢谢!

1 个答案:

答案 0 :(得分:0)

使用您提供的示例JSON数据,我使用了以下命令:

    tell application "JSON Helper" to read JSON from ¬
        "{
            \"status\": \"ok\",
            \"totalResults\": 10,
            \"articles\": [
                {
                    \"source\": {
                        \"id\": \"bbc-news\",
                        \"name\": \"BBC News\"
                    },
                    \"author\": \"BBC News\",
                    \"title\": \"Trump urges Israeli 'care' on settlements\",
                    \"description\": \"The US president also casts doubt on whether the Palestinians or Israel are ready to talk peace.\",
                    \"url\": \"http://www.bbc.co.uk/news/world-middle-east-43025705\",
                    \"urlToImage\": \"https://ichef.bbci.co.uk/news/1024/branded_news/165CD/production/_99979519_044058382.jpg\",
                    \"publishedAt\": \"2018-02-11T16:47:27Z\"
                }
            ]
        }"

检索AppleScript记录。然而,正如我上面所说的那样漂亮地打印它,您可能已经能够看到articles是一个列表(包含一个项目)。因此,虽然:

    set news to title of articles of result

产生错误,这个:

    set news to title of item 1 of articles of result

检索正确的数据。

相关问题