最低限度使用Jayway JsonPath

时间:2016-04-20 08:14:55

标签: java jsonpath

我正在尝试使用JsonPath从json中找到最少的特定值。

给出以下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

2 个答案:

答案 0 :(得分:0)

只是为了不检查评论的人,

@ cricket_007评论说,这是一个开放的bug,check here

答案 1 :(得分:-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);
}

}

以下是解决方案类::

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;
}
}

在解决方案类中运行main方法,您将找到您想要的内容......

快乐帮助......:)