Split data in JSON file based on a key value using java

 

问题

https://stackoverflow.com/questions/70389908/split-data-in-json-file-based-on-a-key-value-using-java

I have a json file with which has an array of product items i want to split them based on the category of the item, This is my json file looks like,

{

"items":[

{

"item-id": 123,

"name": "Cheese",

"price": 8,

"category": "Dairy"

},

{

"item-id": 124,

"name": "Milk",

"price": 23,

"category": "Dairy"

},

{

"item-id": 125,

"name": "Chicken",

"price": 100,

"category": "Meat"

},

{

"item-id": 126,

"name": "Fish",

"price": 45,

"category": "Meat"

}

]

}

i want to split them like like this,

[

{

"category":"Dairy",

"items":[

{

"item-id": 123,

"name": "Cheese",

"price": 8,

"category": "Dairy"

},

{

"item-id": 124,

"name": "Milk",

"price": 23,

"category": "Dairy"

}

]

},

{

"category":"Meat",

"items":[

{

"item-id": 125,

"name": "Chicken",

"price": 100,

"category": "Meat"

},

{

"item-id": 126,

"name": "Fish",

"price": 45,

"category": "Meat"

}

]

}

]

this is the code i tried so far but can't find way to split them like the way i wanted, I'm using java and also i am new to java

import java.io.*;

import java.util.*;

import org.json.simple.*;

import org.json.simple.parser.*;

public class ReadOrderDetails {

@SuppressWarnings("unused")

public static void main(String[] args) {

JSONParser parser = new JSONParser();

JSONObject subOrder = new JSONObject();

JSONArray gitems = new JSONArray();

JSONArray subOrders = new JSONArray();

try {

Object obj = parser.parse(new FileReader("order-details.json"));

JSONObject jsonObject = (JSONObject)obj;

String orderId = (String)jsonObject.get("orderId");

JSONArray items = (JSONArray)jsonObject.get("items");

@SuppressWarnings("rawtypes")

Iterator iterator = items.iterator();

System.out.println("Order Id:" + orderId);

while(iterator.hasNext()) {

JSONObject item = (JSONObject)iterator.next();

if(subOrders.isEmpty()) {

subOrder.put("category", item.get("category"));

gitems.add(item);

subOrder.put("items", gitems);

subOrders.add(subOrder);

} else {

Iterator subOrdersIterator = subOrders.iterator();

for(int i=0; i<subOrders.size(); i++) {

JSONObject sitem = (JSONObject) subOrdersIterator.next();

if(sitem.get("category") == item.get("category")) {

gitems.add(item);

subOrder.put("items", gitems);

subOrders.add(subOrder);

} else {

subOrder.put("category", item.get("category"));

gitems.add(item);

subOrder.put("items", gitems);

subOrders.add(subOrder);

}

}

}

}

} catch(Exception e) {

e.printStackTrace();

}

System.out.println(subOrders);

}

}

and also i'm getting an error atjava.util.ConcurrentModificationExceptionbut that's not my main question, what i really wnated a way to split them i tried couple of things didn't working

解答

json格式数据,按items数组中的category分组。用Java实现代码较长。用Java 下的开源包 SPL 很容易写,只要1句:


A

1

=json(file("data.json").read()).items.group(#4;~:items)

SPL 提供了 JDBC 供 Java 调用,把上面的脚本存为 group.splx,在 Java 中以存储过程的方式调用脚本文件:

Class.forName("com.esproc.jdbc.InternalDriver");

con= DriverManager.getConnection("jdbc:esproc:local://");

st = con.prepareCall("call group()");
st.execute();

SPL 源代码:https://github.com/SPLWare/esProc

问答搜集