为了账号安全,请及时绑定邮箱和手机立即绑定

在Java中的公共数组中按属性对json对象列表进行分组

在Java中的公共数组中按属性对json对象列表进行分组

哈士奇WWW 2023-09-20 17:30:42
我想问是否可以通过其公共数组内的另一个对象对该对象进行分组。这是 JSON 响应,我需要按程序 id对项目列表进行分组。我试图将它放在 HashMap 上,但效果不佳。{"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"        }    }]}所有评论将受到高度赞赏。提前致谢!
查看完整描述

2 回答

?
慕码人2483693

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();

}


查看完整回答
反对 回复 2023-09-20
?
桃花长相依

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"

} ]


查看完整回答
反对 回复 2023-09-20
  • 2 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信