首页 > 解决方案 > 将在 Apex 销售人员闪电中反序列化的 Json 响应

问题描述

我通过点击 RestAPI 得到以下 Json 响应。

**

[
    {
        "index": 0,
        "object": {
            "attributes": {
                "ID": [
                    {
                        "type": "configuration/entityTypes/Customer/attributes/ID",
                        "ov": true,
                        "value": "5",
                        "uri": "entities/attributes/ID/2Cqf5xG2"
                    },
                    {
                        "type": "configuration/entityTypes/Customer/attributes/ID",
                        "ov": true,
                        "value": "3028",
                        "uri": "entities/attributes/ID/fto38ZRR"
                    }
                ],
                "IgpId": [
                    {
                        "type": "configuration/entityTypes/Customer/attributes/IgpId",
                        "ov": true,
                        "value": "1911",
                        "uri": "entities/attributes/IgpId/20ft8omYM"
                    }
                ],
                "IgpName": [
                    {
                        "type": "configuration/entityTypes/Customer/attributes/IgpName",
                        "ov": true,
                        "value": "Dr. heath",
                        "uri": "entities/attributes/IgpName/20ft8oe1q"
                    }
                ],
                "IgpID": [
                    {
                        "type": "configuration/entityTypes/Customer/attributes/IgpID",
                        "ov": true,
                        "value": "1872",
                        "uri": "entities/attributes/IgpID/20ft8oiI6"
                    }
                ],
                "IgpNAME": [
                    {
                        "type": "configuration/entityTypes/Customer/attributes/IgpNAME",
                        "ov": true,
                        "value": "Dr Jc crook",
                        "uri": "entities/attributes/IgpNAME/20ft8oqoc"
                    }
                ]
            }
        },
        "successful": true
    }
]

**

我正在尝试将 CoachID (3028)、IgpID(1872)、IgpId(1911)、IgpName(Dr. heath)、IgpNAME(Dr Jc crook) 的值保存在 APex 类的字符串变量中。

我已经使用地图来执行此操作,但无法获得正确的值。我想在 List 中获取上述值并从函数中返回该列表。

谁可以帮我这个事?我不应该使用包装器来做到这一点。

问候,莫希特

标签: javasalesforceapexsalesforce-lightningaura-framework

解决方案


它不能作为直接的“JSON.deserialize”调用来完成,因为某些字段是 Apex 中的保留关键字。包装禁令有多严格?我不想徒手输入这些东西,但你可以去https://json2apex.herokuapp.com/,勾选关于显式解析器的复选框(再次,因为你已经保留了关键字,它无论如何都会发生),好吧,你有单元测试/示例如何使用代码的现成课程和酷刺。它经过了实战考验,如果你不能使用它,那么你的项目就会有更大的问题;)

如果您真的必须独自完成并使用地图,它将类似于https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm,例如deserializeUntyped


推荐阅读