2016-04-20 49 views
2

我想從給json使用JsonPath找到最小的特定值。最小使用Jayway JsonPath

鑑於以下JSON(從here拍攝):

{ 
    "store": { 
     "book": [{ 
      "category": "fiction", 
      "author": "Herman Melville", 
      "title": "Moby Dick", 
      "isbn": "0-553-21311-3", 
      "price": 8.99 
     }, { 
      "category": "fiction", 
      "author": "J. R. R. Tolkien", 
      "title": "The Lord of the Rings", 
      "isbn": "0-395-19395-8", 
      "price": 22.99 
     }] 
    }, 
    "expensive": 10 
    } 

我試圖找出所有書的價格最低。所以基本上我期待:

 min(8.99,22.99) = 8.99 

通過看一下函數從jsonPath github page例子,我想下面的表達式,但它似乎並不管用。

  $..book[*].price.min() 

,但我得到了以下錯誤:

Aggregation function attempted to calculate value using empty array 

難道我做錯了什麼?

什麼是正確的方式來做這樣的事情?

編輯:您可以測試表達式here

+0

你會得到什麼?簡單的表達式工作嗎? – weston

+0

'store'是json對象,'book'是json數組,因爲它們很小巧 –

+1

非常奇怪,'$ .. book [*]。price'給出了一個數組數組,但添加了'.min()'不起作用。 – weston

回答

-1

以下是您的解決方案寫在解決方案類...

import java.util.ArrayList; 
import com.google.gson.GsonBuilder; 

public class Solution { 

public static void main(String[] args) { 
    //building your object 
    SolutionTo objSolutionTo = new SolutionTo(); 
    StoreTO objStoreTO = new StoreTO(); 
    ArrayList<BookTo> objArrayList = new ArrayList<BookTo>(); 
    BookTo objBookTo = new BookTo(); 
    objBookTo.setAuthor("Herman Melville"); 
    objBookTo.setCategory("fiction"); 
    objBookTo.setTitle("Moby Dick"); 
    objBookTo.setIsbn("0-553-21311-3"); 
    objBookTo.setPrice(8.99); 
    BookTo objBookTo2 = new BookTo(); 
    objBookTo2.setAuthor("J. R. R. Tolkien"); 
    objBookTo2.setCategory("fiction"); 
    objBookTo2.setTitle("The Lord of the Rings"); 
    objBookTo2.setIsbn("0-553-21311-3"); 
    objBookTo2.setPrice(22.99); 
    objArrayList.add(objBookTo); 
    objArrayList.add(objBookTo2); 
    objStoreTO.setBook(objArrayList); 
    objSolutionTo.setStore(objStoreTO); 
    objSolutionTo.setExpensive(10.0); 
    System.out.println(new GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(objSolutionTo)); 


    //The logic you require to find the min value of price 
    Double minPrice = Double.MAX_VALUE; 
    for (BookTo bookTo : objSolutionTo.getStore().getBook()) { 
     if (minPrice > bookTo.getPrice()) 
      minPrice = bookTo.getPrice(); 
    } 
    System.out.println(minPrice); 
} 

}

以下是solutionTO類::

public class SolutionTo { 
private StoreTO store; 
private Double expensive; 

public StoreTO getStore() { 
    return store; 
} 

public void setStore(StoreTO store) { 
    this.store = store; 
} 

public Double getExpensive() { 
    return expensive; 
} 

public void setExpensive(Double expensive) { 
    this.expensive = expensive; 
} 

} 

以下是StoreTo類::

import java.util.ArrayList; 

public class StoreTO { 
private ArrayList<BookTo> book; 

public ArrayList<BookTo> getBook() { 
    return book; 
} 

public void setBook(ArrayList<BookTo> book) { 
    this.book = book; 
} 

} 

以下是bookTo對象::

public class BookTo { 
private String category; 
private String author; 
private String title; 
private String isbn; 
private Double price; 

public String getCategory() { 
    return category; 
} 

public void setCategory(String category) { 
    this.category = category; 
} 

public String getAuthor() { 
    return author; 
} 

public void setAuthor(String author) { 
    this.author = author; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getIsbn() { 
    return isbn; 
} 

public void setIsbn(String isbn) { 
    this.isbn = isbn; 
} 

public Double getPrice() { 
    return price; 
} 

public void setPrice(Double price) { 
    this.price = price; 
} 
} 

運行的解決方案類,你會發現你想要的東西的主要方法...

快樂幫助... :)

+1

@karthik對您有幫助嗎? – Abhishek

+0

@karthik我沒有要求這個代碼。 – Karthik

0

只是爲了誰不檢查意見的人着想,

as @ cricket_007評論說,這是一個開放的bug,check here