mac 下使用shell 命令 jq 解析json


 

官網
https://stedolan.github.io/jq/download/


安裝
brew install jq


創建json文件,file.json
{
    "code": 200,
    "msg": "請求成功",
    "data": {
        "username": "abc",
        "mobile": "18611751121"
    },
    "task":[{
        "username": "abc1",
        "mobile": "18611751121"
    },
    {
        "username": "abc2",
        "mobile": "18611751121"
    }]
}


獲取value
(testenv3.7) localhost:testenv3.7 macname$ jq .msg file.json 
"請求成功"
(testenv3.7) localhost:testenv3.7 macname$ 



獲取value
(testenv3.7) localhost:testenv3.7 macname$ jq .code file.json 
200
(testenv3.7) localhost:testenv3.7 macname$ 



獲取value
(testenv3.7) localhost:testenv3.7 macname$ jq .data file.json 
{
  "username": "abc",
  "mobile": "18611751121"
}
(testenv3.7) localhost:testenv3.7 macname$ 



獲取子對象的值
(testenv3.7) localhost:testenv3.7 macname$ jq .data.username file.json 
"abc"
(testenv3.7) localhost:testenv3.7 macname$ 



獲取數組子元素的值
(testenv3.7) localhost:testenv3.7 macname$ jq .task[0].username file.json 
"abc1"
(testenv3.7) localhost:testenv3.7 macname$ 

(testenv3.7) localhost:testenv3.7 macname$ jq .task[1].username file.json 
"abc2"
(testenv3.7) localhost:testenv3.7 macname$ 

 

 

 

參考:

https://www.linuxidc.com/Linux/2014-03/98041.htm

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM