猿问

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

我想问是否可以通过其公共数组内的另一个对象对该对象进行分组。


这是 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"

        }

    }

]}


所有评论将受到高度赞赏。提前致谢!


哈士奇WWW
浏览 121回答 2
2回答

慕码人2483693

我已经获取了您的源 json 并尝试按照您的规范进行转换,这是有效的解决方案,将您的源 JSON 作为字符串传递,您将获得所需的输出private String parseJson(String source) {&nbsp; &nbsp; JSONArray result = new JSONArray();&nbsp; &nbsp; List<Integer> ids = new ArrayList<>();&nbsp; &nbsp; HashMap<Integer,JSONObject> programs = new HashMap<>();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jSource = new JSONObject(source);&nbsp; &nbsp; &nbsp; &nbsp; JSONArray orderDetails = jSource.getJSONArray("order_details");&nbsp; &nbsp; &nbsp; &nbsp; if (orderDetails.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < orderDetails.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = orderDetails.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject item = jsonObject.getJSONObject("item");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject program = jsonObject.getJSONObject("program");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int programId = jsonObject.getJSONObject("program").getInt("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!ids.contains(programId)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ids.add(programId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; program.put("item",new JSONArray().put(item));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; programs.put(programId,program);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; program.put("item",programs.get(programId).getJSONArray("item").put(item));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int k :programs.keySet()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.put(programs.get(k));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return result.toString();}

桃花长相依

JSON库Josson可以通过简单的表达式转换JSON。https://github.com/octomix/josson反序列化Josson josson = Josson.fromJsonString(&nbsp; &nbsp; "{" +&nbsp; &nbsp; "\"id\": \"\"," +&nbsp; &nbsp; "\"ordered_by\": 64," +&nbsp; &nbsp; "\"order_details\": [" +&nbsp; &nbsp; "&nbsp; &nbsp; {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"resource\": \"Product\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"required_prescription\": false," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"item\": {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"id\": 6," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"name\": \"Synergistic Copper Gloves\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"code\": \"51537661-C\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"enabled\": true," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"generic_name\": \"Mediocre Steel Wallet\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"price_cents\": 200000" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"program\": {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"id\": 12," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"name\": \"Synergistic Wooden Shoes\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"provider\": \"Synergistic Rubber Coat\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"discount_type\": \"fixed\"" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }" +&nbsp; &nbsp; "&nbsp; &nbsp; }," +&nbsp; &nbsp; "&nbsp; &nbsp; {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"resource\": \"Product\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"required_prescription\": true," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"item\": {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"id\": 7," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"name\": \"Rustic Leather Table\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"code\": \"74283131-P\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"enabled\": true," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"generic_name\": \"Incredible Bronze Clock\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"price_cents\": 8994" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"program\": {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"id\": 12," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"name\": \"Synergistic Wooden Shoes\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"provider\": \"Synergistic Rubber Coat\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"discount_type\": \"fixed\"" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }" +&nbsp; &nbsp; "&nbsp; &nbsp; }," +&nbsp; &nbsp; "&nbsp; &nbsp; {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"resource\": \"Product\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"required_prescription\": false," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"item\": {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"id\": 116," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"name\": \"Ergonomic Marble Hat\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"code\": \"98845056-A\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"enabled\": true," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"generic_name\": \"Incredible Granite Lamp\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"price_cents\": 8267" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"program\": {" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"id\": 10," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"name\": \"Durable Rubber Bag\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"provider\": \"Aerodynamic Steel Chair\"," +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"discount_type\": \"fixed\"" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }" +&nbsp; &nbsp; "&nbsp; &nbsp; }" +&nbsp; &nbsp; "]}");转型JsonNode node = josson.getNode(&nbsp; &nbsp; "order_details.group(program, item).field(program:, **:program)");System.out.println(node.toPrettyString());输出[ {&nbsp; "item" : [ {&nbsp; &nbsp; "id" : 6,&nbsp; &nbsp; "name" : "Synergistic Copper Gloves",&nbsp; &nbsp; "code" : "51537661-C",&nbsp; &nbsp; "enabled" : true,&nbsp; &nbsp; "generic_name" : "Mediocre Steel Wallet",&nbsp; &nbsp; "price_cents" : 200000&nbsp; }, {&nbsp; &nbsp; "id" : 7,&nbsp; &nbsp; "name" : "Rustic Leather Table",&nbsp; &nbsp; "code" : "74283131-P",&nbsp; &nbsp; "enabled" : true,&nbsp; &nbsp; "generic_name" : "Incredible Bronze Clock",&nbsp; &nbsp; "price_cents" : 8994&nbsp; } ],&nbsp; "id" : 12,&nbsp; "name" : "Synergistic Wooden Shoes",&nbsp; "provider" : "Synergistic Rubber Coat",&nbsp; "discount_type" : "fixed"}, {&nbsp; "item" : [ {&nbsp; &nbsp; "id" : 116,&nbsp; &nbsp; "name" : "Ergonomic Marble Hat",&nbsp; &nbsp; "code" : "98845056-A",&nbsp; &nbsp; "enabled" : true,&nbsp; &nbsp; "generic_name" : "Incredible Granite Lamp",&nbsp; &nbsp; "price_cents" : 8267&nbsp; } ],&nbsp; "id" : 10,&nbsp; "name" : "Durable Rubber Bag",&nbsp; "provider" : "Aerodynamic Steel Chair",&nbsp; "discount_type" : "fixed"} ]
随时随地看视频慕课网APP

相关分类

Java
我要回答