2016-02-28 58 views
10

我有一個數組:通過與JQ陣列爲JSON(在命令行上)循環

[ 
    { 
     "AssetId": 14462955, 
     "Name": "Cultural Item", 
     "Description": "It\u0027s... you know... an item. People love items!!!", 
     "AbsoluteUrl": "http://www.roblox.com/Cultural-Item-item?id=14462955", 
     "PriceInRobux": "300", 
     "PriceInTickets": "", 
     "Updated": "3 years ago", 
     "Favorited": "370 times", 
     "Sales": "55", 
     "Remaining": "", 
     "Creator": "ROBLOX", 
     "CreatorAbsoluteUrl": "http://www.roblox.com/users/1/profile", 
     "PrivateSales": "3 Private Sellers", 
     "PriceView": 1, 
     "BestPrice": "666,666,666", 
     "ContentRatingTypeID": 0, 
     "IsServerSideThumbnailLookupInCatalogEnabled": true, 
     "IsLargeItem": false, 
     "IsThumbnailFinal": true, 
     "IsThumbnailUnapproved": false, 
     "ThumbnailUrl": "http://t3.rbxcdn.com/cfbab57cabc6090d078c5f7af403caaa", 
     "BcOverlayUrl": null, 
     "LimitedOverlayUrl": "http://images.rbxcdn.com/793dc1fd7562307165231ca2b960b19a.png", 
     "DeadlineOverlayUrl": null, 
     "LimitedAltText": "Limited", 
     "NewOverlayUrl": null, 
     "SaleOverlayUrl": null, 
     "IosOverlayUrl": null, 
     "XboxOverlayUrl": null, 
     "IsTransparentBackground": false, 
     "AssetTypeID": 8, 
     "CreatorID": 1, 
     "CreatedDate": "\/Date(1250206710313)\/", 
     "UpdatedDate": "\/Date(1340756728887)\/", 
     "IsForSale": true, 
     "IsPublicDomain": false, 
     "IsLimited": true, 
     "IsLimitedUnique": false, 
     "MinimumMembershipLevel": 0 
    }, 
    { 
     "AssetId": 114385498, 
     "Name": "Redspybot", 
     "Description": "How could something so conspicuous be a spy? Beats me! This item was awarded to users who redeemed a ROBLOX card from F.Y.E. in May 2013.", 
     "AbsoluteUrl": "http://www.roblox.com/Redspybot-item?id=114385498", 
     "PriceInRobux": "", 
     "PriceInTickets": "", 
     "Updated": "2 years ago", 
     "Favorited": "218 times", 
     "Sales": "0", 
     "Remaining": "", 
     "Creator": "ROBLOX", 
     "CreatorAbsoluteUrl": "http://www.roblox.com/users/1/profile", 
     "PrivateSales": "2 Private Sellers", 
     "PriceView": 1, 
     "BestPrice": "862,133,799", 
     "ContentRatingTypeID": 0, 
     "IsServerSideThumbnailLookupInCatalogEnabled": true, 
     "IsLargeItem": false, 
     "IsThumbnailFinal": true, 
     "IsThumbnailUnapproved": false, 
     "ThumbnailUrl": "http://t3.rbxcdn.com/173f5636efac35199e40740b3839b3d3", 
     "BcOverlayUrl": null, 
     "LimitedOverlayUrl": "http://images.rbxcdn.com/793dc1fd7562307165231ca2b960b19a.png", 
     "DeadlineOverlayUrl": null, 
     "LimitedAltText": "Limited", 
     "NewOverlayUrl": null, 
     "SaleOverlayUrl": null, 
     "IosOverlayUrl": null, 
     "XboxOverlayUrl": null, 
     "IsTransparentBackground": false, 
     "AssetTypeID": 8, 
     "CreatorID": 1, 
     "CreatedDate": "\/Date(1367270100170)\/", 
     "UpdatedDate": "\/Date(1370113730937)\/", 
     "IsForSale": false, 
     "IsPublicDomain": false, 
     "IsLimited": true, 
     "IsLimitedUnique": false, 
     "MinimumMembershipLevel": 0 
    }, 
    { 
     "AssetId": 29715011, 
     "Name": "American Cowboy", 
     "Description": "...or Cowgirl! You know how to rope in some lookieloos to your America place. Yee haw! Rating prize for the July 4, 2010 Building Contest.", 
     "AbsoluteUrl": "http://www.roblox.com/American-Cowboy-item?id=29715011", 
     "PriceInRobux": "", 
     "PriceInTickets": "", 
     "Updated": "3 years ago", 
     "Favorited": "507 times", 
     "Sales": "0", 
     "Remaining": "", 
     "Creator": "ROBLOX", 
     "CreatorAbsoluteUrl": "http://www.roblox.com/users/1/profile", 
     "PrivateSales": "3 Private Sellers", 
     "PriceView": 1, 
     "BestPrice": "950,000,000", 
     "ContentRatingTypeID": 0, 
     "IsServerSideThumbnailLookupInCatalogEnabled": true, 
     "IsLargeItem": false, 
     "IsThumbnailFinal": true, 
     "IsThumbnailUnapproved": false, 
     "ThumbnailUrl": "http://t2.rbxcdn.com/972ae5b4376523dcfdfdcd16c1cef90d", 
     "BcOverlayUrl": null, 
     "LimitedOverlayUrl": "http://images.rbxcdn.com/793dc1fd7562307165231ca2b960b19a.png", 
     "DeadlineOverlayUrl": null, 
     "LimitedAltText": "Limited", 
     "NewOverlayUrl": null, 
     "SaleOverlayUrl": null, 
     "IosOverlayUrl": null, 
     "XboxOverlayUrl": null, 
     "IsTransparentBackground": false, 
     "AssetTypeID": 8, 
     "CreatorID": 1, 
     "CreatedDate": "\/Date(1277767354190)\/", 
     "UpdatedDate": "\/Date(1277769099607)\/", 
     "IsForSale": false, 
     "IsPublicDomain": false, 
     "IsLimited": true, 
     "IsLimitedUnique": false, 
     "MinimumMembershipLevel": 0 
    }, 
    { 
     "AssetId": 98253651, 
     "Name": "Mahem\u0026#39;s ROBLOX Tablet", 
     "Description": "Winner: Mahem. ROBLOX will be in touch to redeem this prize. Void if sold, traded, or deleted.", 
     "AbsoluteUrl": "http://www.roblox.com/Mahems-ROBLOX-Tablet-item?id=98253651", 
     "PriceInRobux": "90,000", 
     "PriceInTickets": "", 
     "Updated": "3 years ago", 
     "Favorited": "429 times", 
     "Sales": "1", 
     "Remaining": "0", 
     "Creator": "ROBLOX", 
     "CreatorAbsoluteUrl": "http://www.roblox.com/users/1/profile", 
     "PrivateSales": "1 Private Sellers", 
     "PriceView": 1, 
     "BestPrice": "999,999,999", 
     "ContentRatingTypeID": 0, 
     "IsServerSideThumbnailLookupInCatalogEnabled": true, 
     "IsLargeItem": false, 
     "IsThumbnailFinal": true, 
     "IsThumbnailUnapproved": false, 
     "ThumbnailUrl": "http://t7.rbxcdn.com/1dc64223edd569ea698191c86c124928", 
     "BcOverlayUrl": "http://images.rbxcdn.com/4fc3a98692c7ea4d17207f1630885f68.png", 
     "LimitedOverlayUrl": "http://images.rbxcdn.com/d649b9c54a08dcfa76131d123e7d8acc.png", 
     "DeadlineOverlayUrl": null, 
     "LimitedAltText": "Limited Unique", 
     "NewOverlayUrl": null, 
     "SaleOverlayUrl": null, 
     "IosOverlayUrl": null, 
     "XboxOverlayUrl": null, 
     "IsTransparentBackground": false, 
     "AssetTypeID": 19, 
     "CreatorID": 1, 
     "CreatedDate": "\/Date(1353368761747)\/", 
     "UpdatedDate": "\/Date(1355363144050)\/", 
     "IsForSale": true, 
     "IsPublicDomain": false, 
     "IsLimited": false, 
     "IsLimitedUnique": true, 
     "MinimumMembershipLevel": 1 
    } 
] 

我想通過這個陣列中的每個對象來循環,並挑選出每個鍵的所謂的值AssetIdconsole.log()它。 我該如何在命令行中使用jq來做到這一點?

+0

請更清楚它是否是一個[JQ]的問題或[jQuery的]的問題。他們不是一樣的,不應該一起使用......你的問題在你如何標記和詢問它的方式上完全不明確。 –

回答

12

命令行工具jq寫入STDOUT和/或STDERR。如果你想要寫的.AssetId信息輸出到標準輸出,那麼一種可能是如下:

jq -r ".[] | .AssetId" input.json 

輸出:

14462955 
114385498 
29715011 
98253651 

一個更強大的咒語是:.[] | .AssetId?,但你的選擇將取決於如果沒有名爲「AssetId」的鍵,您需要什麼。

+0

謝謝,非常有幫助的答案! –

+0

注意:如何將每個函數發送到一個函數? –

+0

嘗試此操作後,我發現一個錯誤。它說意外的字符串。 –

0

你也可以通過這個命令來完成。

jq ".[].AssetId" input.json 

如果數組一樣是這是在我的情況

{ 
    "resultCode":0, 
    "resultMsg":"SUCCESS", 
    "uniqueRefNo":"111222333", 
    "list":[ 
     { 
     "cardType":"CREDIT CARD", 
     "isBusinessCard":"N", 
     "memberName":"Bank A", 
     "memberNo":10, 
     "prefixNo":404591 
     }, 
     { 
     "cardType":"DEBIT CARD", 
     "isBusinessCard":"N", 
     "memberName":"Bank A", 
     "memberNo":10, 
     "prefixNo":407814 
     }, 
     { 
     "cardType":"CREDIT CARD", 
     "isBusinessCard":"N", 
     "memberName":"Bank A", 
     "memberNo":10, 
     "prefixNo":413226 
     } 
    ] 
} 

你可以用下面的命令JQ拿到prefixNo。

jq ".list[].prefixNo" input.json 

有關陣列更具體的情況下迭代上JQ你可以檢查this blogpost