2015-06-21 36 views
1

的第一次出現所以我有一個JSON文件是這樣的:JSONPath「採集」

{ 
    "cards": [ 
    { 
     "artist": "Steve Argyle", 
     "images": { 
     "mtgimage": "http://mtgimage.com/set/pMPR/ponder.jpg" 
     }, 
    }, 
    { 
     "artist": "Mark Tedin", 
     "images": { 
     "gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=139512", 
     "mtgimage": "http://mtgimage.com/set/LRW/ponder.jpg" 
     }, 
    }, 
    { 
     "artist": "Dan Scott", 
     "images": { 
     "gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=190159", 
     "mtgimage": "http://mtgimage.com/set/M10/ponder.jpg" 
     }, 
    } 
    ] 
} 

我想用JSONPath從它那裏得到的第一個「採集」鏈接。 我試過「$ .. gatherer [0]」,但這不起作用。然而,「$ .. gatherer」的確提供了兩種情況:

[ 
    "http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=139512", 
    "http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=190159" 
] 

我如何獲得第一個? (沒有得到在代碼中的最後一個,所以只有使用jsonpath字符串)。

(測試用http://jsonpath.curiousconcept.com/,在我的計劃。)

回答

0

您可能需要一箇中間步驟的陣列

var gatherers = $..gatherer; //however this gets it, I'm not sure 
var first = gatherers[0]; 
保存

可能工作。

+0

這應該確實有效。不過,我正在尋找一個僅限jsonpath的解決方案。這個想法是用戶可以輸入他們自己的jsonpath字符串。獲得第一個「收集者」鏈接是我期望需要使用的。所以我希望有一個不需要代碼的解決方案,只需要一個jsonpath字符串即可完成。 –