码迷,mamicode.com
首页 > Web开发 > 详细

Curl 与jq 发起json请求

时间:2020-02-05 23:02:12      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:c99   lis   tran   action   命令行   格式化json   行操作   des   访问   

1. 安装

官网:https://stedolan.github.io/jq/download/
基本就是brew install、apt install、yum install之类的,很简单

2. 常用操作

2.1 格式化json

直接请求,格式明显不易读

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc
{"_links":{"next":{"href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=desc"},"prev":{"href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=asc"}},"_embedded":{"records":[{"memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=","id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e","paging_token":"120624397026840576","successful":true,"hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e","ledger":28085056,"created_at":"2020-02-05T13:01:24Z","source_account_sequence":"112094093760491433","fee_paid":100,"fee_charged":100,"max_fee":100,"operation_count":1,"result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=","memo_type":"hash","valid_after":"1970-01-01T00:00:00Z"}]}}

使用jq格式化

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq 
{
    "_links":{
        "next":{
            "href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=desc"
        },
        "prev":{
            "href":"https://horizon.stellar.org/transactions?cursor=120624397026840576&limit=1&order=asc"
        }
    },
    "_embedded":{
        "records":[
            {
                "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
                "id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "paging_token":"120624397026840576",
                "successful":true,
                "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "ledger":28085056,
                "created_at":"2020-02-05T13:01:24Z",
                "source_account_sequence":"112094093760491433",
                "fee_paid":100,
                "fee_charged":100,
                "max_fee":100,
                "operation_count":1,
                "result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
                "memo_type":"hash",
                "valid_after":"1970-01-01T00:00:00Z"
            }
        ]
    }
}

2.2 取指定字段的值

直接输入字段,使用.嵌套访问,例如要获取records

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq ._embedded.records
[
            {
                "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
                "id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "paging_token":"120624397026840576",
                "successful":true,
                "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "ledger":28085056,
                "created_at":"2020-02-05T13:01:24Z",
                "source_account_sequence":"112094093760491433",
                "fee_paid":100,
                "fee_charged":100,
                "max_fee":100,
                "operation_count":1,
                "result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
                "memo_type":"hash",
                "valid_after":"1970-01-01T00:00:00Z"
            }
]

2.3 过滤指定字段

使用一个json指定所有字段,如{memo, hash},获取昨天的最高温度如下

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq ._embedded.records | jq ".[0]" | jq "{memo, hash}"
{
  "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
  "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e"
}

2.4 获取多个字段的值

使用逗号获取多个

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc | jq "._embedded.records[0]" | jq ".memo, .hash"

"AaUJ0NXa2UE1j4QiOfgy5qqhD8oT8kWI9Cy4xfvb/u8="
"fc3e37d08c99496468f03adbb01f0c32b614f2423508489456c5a32157622036"

2.5 筛选数组

直接指定数组的索引即可

$ curl -s https://horizon.stellar.org/transactions?limit=1&order=desc| jq "._embedded.records" | jq ".[0, 9]"
{
                "memo":"MmFiOTViOTFmNzQ0MmUwY2EzY2I4NTIzZDIwYzFmNWE=",
                "id":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "paging_token":"120624397026840576",
                "successful":true,
                "hash":"da7dee6d1d9374b726e77e6aa321141cd22d679dc3d40ffdbb3cbfcbae93bd3e",
                "ledger":28085056,
                "created_at":"2020-02-05T13:01:24Z",
                "source_account_sequence":"112094093760491433",
                "fee_paid":100,
                "fee_charged":100,
                "max_fee":100,
                "operation_count":1,
                "result_xdr":"AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAA=",
                "memo_type":"hash",
                "valid_after":"1970-01-01T00:00:00Z"
}
...

3. 参考

Curl 与jq 发起json请求

标签:c99   lis   tran   action   命令行   格式化json   行操作   des   访问   

原文地址:https://www.cnblogs.com/yueyun00/p/12267063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!