2 回答
TA贡献1860条经验 获得超9个赞
我已经获取了您的源 json 并尝试按照您的规范进行转换,这是有效的解决方案,将您的源 JSON 作为字符串传递,您将获得所需的输出
private String parseJson(String source) {
JSONArray result = new JSONArray();
List<Integer> ids = new ArrayList<>();
HashMap<Integer,JSONObject> programs = new HashMap<>();
try {
JSONObject jSource = new JSONObject(source);
JSONArray orderDetails = jSource.getJSONArray("order_details");
if (orderDetails.length() > 0) {
for (int i = 0; i < orderDetails.length(); i++) {
JSONObject jsonObject = orderDetails.getJSONObject(i);
JSONObject item = jsonObject.getJSONObject("item");
JSONObject program = jsonObject.getJSONObject("program");
int programId = jsonObject.getJSONObject("program").getInt("id");
if (!ids.contains(programId)) {
ids.add(programId);
program.put("item",new JSONArray().put(item));
programs.put(programId,program);
}else{
program.put("item",programs.get(programId).getJSONArray("item").put(item));
}
}
for(int k :programs.keySet()){
result.put(programs.get(k));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
TA贡献1860条经验 获得超8个赞
JSON库Josson可以通过简单的表达式转换JSON。
https://github.com/octomix/josson
反序列化
Josson josson = Josson.fromJsonString(
"{" +
"\"id\": \"\"," +
"\"ordered_by\": 64," +
"\"order_details\": [" +
" {" +
" \"resource\": \"Product\"," +
" \"required_prescription\": false," +
" \"item\": {" +
" \"id\": 6," +
" \"name\": \"Synergistic Copper Gloves\"," +
" \"code\": \"51537661-C\"," +
" \"enabled\": true," +
" \"generic_name\": \"Mediocre Steel Wallet\"," +
" \"price_cents\": 200000" +
" }," +
" \"program\": {" +
" \"id\": 12," +
" \"name\": \"Synergistic Wooden Shoes\"," +
" \"provider\": \"Synergistic Rubber Coat\"," +
" \"discount_type\": \"fixed\"" +
" }" +
" }," +
" {" +
" \"resource\": \"Product\"," +
" \"required_prescription\": true," +
" \"item\": {" +
" \"id\": 7," +
" \"name\": \"Rustic Leather Table\"," +
" \"code\": \"74283131-P\"," +
" \"enabled\": true," +
" \"generic_name\": \"Incredible Bronze Clock\"," +
" \"price_cents\": 8994" +
" }," +
" \"program\": {" +
" \"id\": 12," +
" \"name\": \"Synergistic Wooden Shoes\"," +
" \"provider\": \"Synergistic Rubber Coat\"," +
" \"discount_type\": \"fixed\"" +
" }" +
" }," +
" {" +
" \"resource\": \"Product\"," +
" \"required_prescription\": false," +
" \"item\": {" +
" \"id\": 116," +
" \"name\": \"Ergonomic Marble Hat\"," +
" \"code\": \"98845056-A\"," +
" \"enabled\": true," +
" \"generic_name\": \"Incredible Granite Lamp\"," +
" \"price_cents\": 8267" +
" }," +
" \"program\": {" +
" \"id\": 10," +
" \"name\": \"Durable Rubber Bag\"," +
" \"provider\": \"Aerodynamic Steel Chair\"," +
" \"discount_type\": \"fixed\"" +
" }" +
" }" +
"]}");
转型
JsonNode node = josson.getNode(
"order_details.group(program, item).field(program:, **:program)");
System.out.println(node.toPrettyString());
输出
[ {
"item" : [ {
"id" : 6,
"name" : "Synergistic Copper Gloves",
"code" : "51537661-C",
"enabled" : true,
"generic_name" : "Mediocre Steel Wallet",
"price_cents" : 200000
}, {
"id" : 7,
"name" : "Rustic Leather Table",
"code" : "74283131-P",
"enabled" : true,
"generic_name" : "Incredible Bronze Clock",
"price_cents" : 8994
} ],
"id" : 12,
"name" : "Synergistic Wooden Shoes",
"provider" : "Synergistic Rubber Coat",
"discount_type" : "fixed"
}, {
"item" : [ {
"id" : 116,
"name" : "Ergonomic Marble Hat",
"code" : "98845056-A",
"enabled" : true,
"generic_name" : "Incredible Granite Lamp",
"price_cents" : 8267
} ],
"id" : 10,
"name" : "Durable Rubber Bag",
"provider" : "Aerodynamic Steel Chair",
"discount_type" : "fixed"
} ]
添加回答
举报