从 Java 中的大型 JSON 文件中提取特定文本(密码字符串),而无需使用 JSON 帮助程序类

我有以下从特定 REST 调用收到的大型 JSON。我只想提取密码字符串(以粗体突出显示)。所以我需要这些值。, , , , , 在 java 中。我想我将不得不使用字符串正则表达式进行搜索。有人可以帮忙吗?ConfigPWD_USER.passwordConfigPWD_ROOT.passwordinstanceOwnerPassworddatabaseUserPasswordfencedUserPassword


{

    "deployment_name": "name1",

    "model": {

        "model": {

            "description": "",

            "nodes": [{

                "id": "OS Node",

                "ptype": "image:OS Node",

                "attributes": {

                    <more json key value pairs>

                    **"ConfigPWD_USER.password": "<xor>NzozMzA=",

                    "ConfigPWD_ROOT.password": "<xor>NzozMzA="**

                },

                "type": "image:OS,

                "groups": {},

                "locked": []

            }, {

                "id": "disk for db instance",

                "attributes": {

                    <more json key value pairs>

                },

                "type": "add disk:1.0.0",

                "groups": {},

                "locked": []

            }, {

                "id": "disk for database data",

                "attributes": {

                    <more json key value pair>

                },

                "type": "disk:1.0.0",

                "groups": {},

                "locked": []

            }, {

                "id": "DB Server",

                "startsAfter": ["disk for db instance",

                    "OS Node"

                ],

                "locked": ["instanceMountPoint", "instanceMountPoint", "instanceMountPoint"],

                "attributes": {

                    "instanceMountPoint": "${disk for db instance.MOUNT_POINT}",

                    <more json key value pair>

                    **"instanceOwnerPassword": "<xor>NzozMzA=",**

                    <more json key value pair>

                    **"fencedUserPassword": "<xor>NzozMzA=",**


                },

                "type": "DBLUN",

                "groups": {}

            }



收到一只叮咚
浏览 118回答 1
1回答

慕仙森

在groovy中有内置和代码来查找其中一个值,可能是这样的:groovy.json.JsonSlurperdef json = new groovy.json.JsonSlurper().parseText('''{&nbsp; &nbsp; "deployment_name": "name1",&nbsp; &nbsp; "model": {&nbsp; &nbsp; &nbsp; &nbsp; "model": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "description": "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "nodes": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "OS Node",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ptype": "image:OS Node",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "attributes": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ConfigPWD_USER.password": "1<xor>NzozMzA=",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ConfigPWD_ROOT.password": "1<xor>NzozMzA="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type": "image:OS",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "groups": {},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "locked": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "disk for db instance",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "attributes": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ConfigPWD_USER.password": "2<xor>NzozMzA=",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ConfigPWD_ROOT.password": "2<xor>NzozMzA="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type": "add disk:1.0.0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "groups": {},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "locked": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;''')json.model.model.nodes.collect{n-> n.attributes."ConfigPWD_USER.password" }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java