简体   繁体   English

使用Powershell工具Invoke-RestMethod从api提取数据

[英]Extracting data from api using powershell tool Invoke-RestMethod

I am trying to get JSON details from an api using powershell. 我正在尝试使用Powershell从api获取JSON详细信息。 I do not have any expereince with power shell, but I guess Invoke-RestMethod would be helpful. 我对Power Shell没有任何经验,但是我想Invoke-RestMethod会有所帮助。

the api I am trying to use is : 我尝试使用的api是:

api.spotcrime.com/crimes.json?lat=30.639155&lon=-96.3647937&radius=0.02&key=spotcrime-private-api-key api.spotcrime.com/crimes.json?lat=30.639155&lon=-96.3647937&radius=0.02&key=spotcrime-private-api-key

Please read the online documentation: Invoke-RestMethod 请阅读在线文档: Invoke-RestMethod

Here is a sample showing how to access the data returned from this API: 这是显示如何访问此API返回的数据的示例:

$uri = 'api.spotcrime.com/crimes.json'

$params = @{
    lat = '30.639155'
    lon = '-96.3647937'
    radius = '0.02'
    key = 'spotcrime-private-api-key'
}

$crimes = Invoke-RestMethod -Uri $uri -Body $params | 
    Select-Object -ExpandProperty crimes

$crimes | Format-Table -AutoSize

In this dataset we have an object named crimes which contains an array of records. 在此数据集中,我们有一个名为Crimes的对象,其中包含一组记录。 Expanding the crimes object and storing it to a $crimes variable will allow us to work with the records more easily. 扩展犯罪对象并将其存储到$crimes变量将使我们能够更轻松地处理记录。

Notice how Invoke-RestMethod automatically detects and converts JSON into objects. 注意Invoke-RestMethod如何自动检测JSON并将其转换为对象。 No need to use ConvertFrom-Json . 无需使用ConvertFrom-Json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM