gpt4 book ai didi

java - 最低使用 Jayway JsonPath

转载 作者:行者123 更新时间:2023-11-29 09:28:30 27 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

以下是你写在解决方案类中的解决方案...

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

运行解决方案类中的主要方法你会找到你想要的...

快乐的帮助...:)

关于java - 最低使用 Jayway JsonPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com