2017-09-15 40 views
0

JSON節點的過濾我有JSON具有這種結構:問題與JsonPath

{ 
    "stores": { 
     "store1_id": { 
      "books": { 
       "book1_ISBN": { 
        "category": "reference", 
        "author": "Nigel Rees", 
        "title": "Sayings of the Century", 
        "price": "8.95" 
       }, 
       "book2_ISBN": { 
        "category": "fiction", 
        "author": "Evelyn Waugh", 
        "title": "Sword of Honour", 
        "price": "12.99" 
       } 
      } 
     }, 
     "store2_id": { 
      "books": { 
       "book3_ISBN": { 
        "category": "fiction", 
        "author": "J. R. R. Tolkien", 
        "title": "The Lord of the Rings", 
        "isbn": "0-395-19395-8", 
        "price": "22.99" 
       } 
      } 
     } 
    } 
} 

和我與Jayway JsonPath解析它。我嘗試用這樣的表達得到與小說類別中的所有書籍:

$[?(@.stores.*.books)].stores.*.books[?(@.*.category=="reference")] 

,但我得到空的結果,雖然根據等於運營商的文檔我期望我會收到「世紀語錄」的書節點:

"book1_ISBN": { 
    "category": "reference", 
    "author": "Nigel Rees", 
    "title": "Sayings of the Century", 
    "price": "8.95" 
} 

有趣的是,這時候我用這句話不等於操作:

$[?(@.stores.*.books)].stores.*.books[?(@.*.category)] 

我得到的所有三本書的結果。爲什麼我的表達與平等運營商不起作用?

回答

1

我在這裏試過你的表情: http://jsonpath.com/? ,他們不工作,他們都與過濾器和沒有過濾器。
我不明白你爲什麼先篩選這些元素:(。@ .stores *書)
無論如何,如果你只是想過濾所有類別的「虛構」的書,這是簡單的:

$..[?(@.category=="fiction")] 

我希望這個幫助。

+0

我在這裏測試了我的表達式:http://jsonpath.herokuapp.com/和沒有等號運算符的表達式在那裏工作,奇怪..無論如何,我嘗試你的一個,它返回正確的結果(兩個節點),謝謝,但我查看結果不包含ISBN鍵 - 我怎樣才能得到它? –

+0

它...這是我所看到的:
[ { 「類別」:「小說」, 「作家」:「伊夫林沃」, 「稱號」:「榮譽之劍」, 「價格」 「12.99」 },{ 「類別」: 「小說」, 「作家」: 「托爾金」, 「稱號」: 「在指環王」, 「ISBN」:「0-395 -19395-8「, 」price「:」22.99「 } ] – marco

+0

我的意思是」books「節點下的鍵,例如」book2_ISBN「,」book3_ISBN「 –