如何使用另一个元素作为键来获取JSON元素?

时间:2016-06-01 17:52:14

标签: json

我有以下json文件,我希望var response = api.ToObject<ResponseModel>(); 包含值 hive-HIVESERVER2 的所有hostId。我正在使用shell脚本,我知道grep工具可以使用但不确定如何。

name

我的输出应该包含像此一样的hostIds

{
    "items":[
        {
            "name":"hive-HIVEMETASTORE-e70a2fe8fd0531ad1f87de49f03537a6",
            "type":"HIVEMETASTORE",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"166219e3-be5c-46d0-b4c7-33543a29ce32"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-HIVEMETASTORE-e70a2fe8fd0531ad1f87de49f03537a6",
            "roleState":"STARTED",
            "healthSummary":"GOOD",
            "healthChecks":[
                {
                    "name":"HIVEMETASTORE_CANARY_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_FILE_DESCRIPTOR",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_HEAP_DUMP_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVEMETASTORE_HOST_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_LOG_DIRECTORY_FREE_SPACE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_PAUSE_DURATION",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_SCM_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_SWAP_MEMORY_USAGE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_UNEXPECTED_EXITS",
                    "summary":"GOOD"
                }
            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-81c55ab9c936d8808fb423fc3f7510e2",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"6c05ac39-076a-4f88-9200-3bfac81552ba"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-81c55ab9c936d8808fb423fc3f7510e2",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-fb6632ccc4ef400e22db19d164c127dd",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"08b76431-45b8-49b1-af8a-61892a982a93"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-fb6632ccc4ef400e22db19d164c127dd",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-598fde9576d5905f01bf1f264a0b7ef7",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"fb0d63bc-5855-47d6-98c5-45b94a0c98cf"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-598fde9576d5905f01bf1f264a0b7ef7",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-79347075ce8ad22356caf866a717f33c",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"fd0356b2-5e47-4805-b941-3b1de4f9bdc8"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-79347075ce8ad22356caf866a717f33c",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-6ba8d81e72663c55f8bb4762e09b1b9b",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"d811ac58-e172-4c89-83e4-b7d182f9f875"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-6ba8d81e72663c55f8bb4762e09b1b9b",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-17fdcdea452dd3ec40fdbef4e2e85496",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"ba31ae57-f935-44eb-87c4-0811e3cbb4f3"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-17fdcdea452dd3ec40fdbef4e2e85496",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-41b46dfe8b50e5cec298037a76508b83",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"3c4fed67-fea7-49c5-be06-a2ee44ef4ba2"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-41b46dfe8b50e5cec298037a76508b83",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-105b51ca279a7d9ae2eacf9134763fdd",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"3ceaba8b-d974-4cfa-b547-89a5c88cee31"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-105b51ca279a7d9ae2eacf9134763fdd",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-HIVESERVER2-851598d434e933382887001430b923ea",
            "type":"HIVESERVER2",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"65ad096f-7cab-4197-8d61-b0a1a67236d4"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-HIVESERVER2-851598d434e933382887001430b923ea",
            "roleState":"STARTED",
            "healthSummary":"GOOD",
            "healthChecks":[
                {
                    "name":"HIVESERVER2_DOWNLOADED_RESOURCES_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVESERVER2_EXEC_LOCAL_SCRATCH_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVESERVER2_FILE_DESCRIPTOR",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_HEAP_DUMP_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVESERVER2_HOST_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_LOG_DIRECTORY_FREE_SPACE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_PAUSE_DURATION",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_SCM_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_SWAP_MEMORY_USAGE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_UNEXPECTED_EXITS",
                    "summary":"GOOD"
                }
            ],
            "configStale":false
        },
        {
            "name":"hive-HIVEMETASTORE-302da147a01099642f81fe0adb73be79",
            "type":"HIVEMETASTORE",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"e6f30663-69b7-4af8-bd97-bca57311745a"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-HIVEMETASTORE-302da147a01099642f81fe0adb73be79",
            "roleState":"STARTED",
            "healthSummary":"GOOD",
            "healthChecks":[
                {
                    "name":"HIVEMETASTORE_CANARY_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_FILE_DESCRIPTOR",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_HEAP_DUMP_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVEMETASTORE_HOST_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_LOG_DIRECTORY_FREE_SPACE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_PAUSE_DURATION",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_SCM_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_SWAP_MEMORY_USAGE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVEMETASTORE_UNEXPECTED_EXITS",
                    "summary":"GOOD"
                }
            ],
            "configStale":false
        },
        {
            "name":"hive-HIVESERVER2-77234341302d577ab6f41383a5052ed9",
            "type":"HIVESERVER2",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"13a308ab-8915-487c-8487-6c0655bab761"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-HIVESERVER2-77234341302d577ab6f41383a5052ed9",
            "roleState":"STARTED",
            "healthSummary":"GOOD",
            "healthChecks":[
                {
                    "name":"HIVESERVER2_DOWNLOADED_RESOURCES_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVESERVER2_EXEC_LOCAL_SCRATCH_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVESERVER2_FILE_DESCRIPTOR",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_HEAP_DUMP_DIRECTORY_FREE_SPACE",
                    "summary":"DISABLED"
                },
                {
                    "name":"HIVESERVER2_HOST_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_LOG_DIRECTORY_FREE_SPACE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_PAUSE_DURATION",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_SCM_HEALTH",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_SWAP_MEMORY_USAGE",
                    "summary":"GOOD"
                },
                {
                    "name":"HIVESERVER2_UNEXPECTED_EXITS",
                    "summary":"GOOD"
                }
            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-966c3832f8d3a1ae83aec73b15dc59d1",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"ad6099b6-e9b1-42aa-a098-c2425876434e"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-966c3832f8d3a1ae83aec73b15dc59d1",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-6af446d294b0e6553164a5bf4460eaba",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"3fa7b40e-9f64-42cc-bf65-c062b1a81d4b"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-6af446d294b0e6553164a5bf4460eaba",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-952f4ac1b24a57969b6cee6454f8f13e",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"b2cc79cf-d5e8-49ab-a69a-8c0802ddc98c"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-952f4ac1b24a57969b6cee6454f8f13e",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-457ad5ebc65b3ac132b6ea2fe7db50aa",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"b2a39623-614d-4cfa-b8fe-98db831c41b8"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-457ad5ebc65b3ac132b6ea2fe7db50aa",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        },
        {
            "name":"hive-GATEWAY-94c574a45b6afb8e80331f6324e223d9",
            "type":"GATEWAY",
            "serviceRef":{
                "clusterName":"cluster",
                "serviceName":"hive"
            },
            "hostRef":{
                "hostId":"888f6ac3-6159-4311-8a67-3adb8fc4af01"
            },
            "roleUrl":"http://localhost:7180/cmf/roleRedirect/hive-GATEWAY-94c574a45b6afb8e80331f6324e223d9",
            "roleState":"NA",
            "healthSummary":"GOOD",
            "healthChecks":[

            ],
            "configStale":false
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

items.forEach(function(item) {
    if ( /* item.name HIVE regex */ ) answerArr.push(item.hostRef.hostId);
}