如何在java中按对象值对JsonNode排序
如何根据产品节点的成本变量升序或降序获得排序值?或者如何通过在下面的JsonNode响应中对产品节点进行排序来修改其值 JsonNode响应:如何在java中按对象值对JsonNode排序,java,arrays,json,sorting,arraylist,Java,Arrays,Json,Sorting,Arraylist,如何根据产品节点的成本变量升序或降序获得排序值?或者如何通过在下面的JsonNode响应中对产品节点进行排序来修改其值 JsonNode响应: { "products": [ { "Name": "Marble", "cost": 47.49 }, { "Name": &
{
"products": [
{
"Name": "Marble",
"cost": 47.49
},
{
"Name": "Mixer",
"cost": 59.99
},
{
"Name": "Soap",
"cost": 3.99
}
],
"details": [
{
"area": 1090.098
}
]
}
{
"products": [
{
"Name": "Mixer",
"cost": 59.99
},
{
"Name": "Marble",
"cost": 47.49
},
{
"Name": "Soap",
"cost": 3.99
}
],
"details": [
{
"area": 1090.098
}
]
}
降序响应除外:
{
"products": [
{
"Name": "Marble",
"cost": 47.49
},
{
"Name": "Mixer",
"cost": 59.99
},
{
"Name": "Soap",
"cost": 3.99
}
],
"details": [
{
"area": 1090.098
}
]
}
{
"products": [
{
"Name": "Mixer",
"cost": 59.99
},
{
"Name": "Marble",
"cost": 47.49
},
{
"Name": "Soap",
"cost": 3.99
}
],
"details": [
{
"area": 1090.098
}
]
}
请指导我使用正确的解决方案,我已经厌倦了使用comparator,但是
JsonNode
响应中的这个产品节点没有任何POJO类,因此我不能使用obj.StringName
进行比较。以下解决方案使用包解析和生成JSON
import java.util.*;
import java.io.*;
import javax.json.*;
public class Products
{
// This is an "internal" method that loads the JSON to a String
static final String json = LFEC.loadFile("products.json");
public static void main(String[] argv) throws IOException
{
// *********** Parse Input Json *****************
// Save the products in a sorted TreeMap...
// NOTE: We will have to "resort it" afterwards..
// **********************************************
TreeMap<String, Double> productsSortedByName = new TreeMap<>();
// Retrieve the JsonObject's
JsonObject obj = Json.createReader(new StringReader(json)).readObject();
JsonArray products = obj.getJsonArray("products");
for (JsonObject product : products.getValuesAs(JsonObject.class))
{
// Retreives the product name and price for each product
String name = product.getString("Name");
double cost = product.getJsonNumber("cost").doubleValue();
// Save in a Java TreeMap<String, Double>
// NOTE: TreeMap sorts by the "Key" (not the value)
productsSortedByName.put(name, Double.valueOf(cost));
}
// ******** Sort Products by Price **************
// Since the question requires that the TreeMap sort by price, not product name...
// This shall build a new TreeMap that is sorted by the Price
// This is tricky, because it requires having **TWO** TreeMap's
// **********************************************
Comparator<String> comp = (String key1, String key2) ->
productsSortedByName.get(key1).compareTo(productsSortedByName.get(key2));
// Build the TreeMap, and put the contents of the original TreeMap into the new one
TreeMap<String, Double> productsSortedByPrice = new TreeMap<>(comp);
productsSortedByPrice.putAll(productsSortedByName);
// ********* Products are Sorted ****************
// Now build the sorted JSON - this part is "self-explanatory."
// AGAIN, this is how the JDK package "javax.json.*" can be used to create JSON
// ********* Build Output Json ******************
JsonArrayBuilder arrBuilder = Json.createArrayBuilder();
for (String product : productsSortedByPrice.keySet())
arrBuilder.add(Json.createObjectBuilder()
.add("name", product)
.add("cost", productsSortedByPrice.get(product).doubleValue()));
String sortedJson = Json
.createObjectBuilder()
.add("products", arrBuilder)
.add("details", obj.getJsonArray("details"))
.build()
.toString();
// Print out the sorted JSON to terminal...
System.out.println(sortedJson);
}
}
如果不想为数据模型创建POJO,可以使用
javax.json.json api
库直接使用json
有关我添加到项目中的依赖项列表,请参见我的maven文件(在代码示例之后)
package so;
import java.io.StringReader;
import java.io.StringWriter;
import javax.json.*;
public class SortExample {
public JsonObject sortProductsByPrice(JsonObject order) {
JsonArray products = order.getJsonArray("products");
JsonArray details = order.getJsonArray("details");
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
products.stream().sorted((product1, product2) -> {
JsonNumber cost1 = product1.asJsonObject().getJsonNumber("cost");
JsonNumber cost2 = product2.asJsonObject().getJsonNumber("cost");
return Double.compare(cost1.doubleValue(), cost2.doubleValue());
}).forEach((value) -> arrayBuilder.add(value));
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
objectBuilder.add("products", arrayBuilder.build());
objectBuilder.add("details", details);
return objectBuilder.build();
}
public static void main(String[] args) {
String orderString =
"{\"products\": [ {\"Name\": \"Marble\",\"cost\": 47.49 }, { \"Name\": \"Mixer\", \"cost\": 59.99 }, { \"Name\": \"Soap\", \"cost\": 3.99 } ], \"details\" : [ { \"area\" : 1090.098 } ] }";
JsonObject order = Json.createReader(new StringReader(orderString)).readObject();
JsonObject sortedOrder = new SortExample().sortProductsByPrice(order);
StringWriter writer = new StringWriter();
Json.createWriter(writer).writeObject(sortedOrder);
System.out.println(writer.toString());
}
}
印刷品:
{"products":[{"Name":"Soap","cost":3.99},{"Name":"Marble","cost":47.49},{"Name":"Mixer","cost":59.99}],"details":[{"area":1090.098}]}
我的示例中的main
方法只是根据您的示例构造有效负载。这项工作是在sortProductsByPrice
中完成的
它只是将产品保留在它们的JsonValue
表示中,并使用流式API对它们进行排序,然后将它们放回一个新的JsonArray
中,最后用原始的细节和排序后的产品构建一个新的JsonObject
我不熟悉javax.json,也找不到一个漂亮的打印选项。也许有更好的图书馆。但这给了你想要做什么的要点
依赖项:
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
javax.json
javax.json-api
1.1.4
玻璃鱼
javax.json
1.1.4