【问题标题】:Converting JSON String to Dictionary in python [duplicate]在python中将JSON字符串转换为字典[重复]
【发布时间】:2016-04-10 06:43:50
【问题描述】:
{
"status_code": 0,
"result_type": "DRAGON_NLU_ASR_CMD",
"NMAS_PRFX_SESSION_ID": "8c63c3ed-40da-4cdc-8ad8-1dd94ce8e466",
"NMAS_PRFX_TRANSACTION_ID": "1",
"audio_transfer_info": {
    "packages": [
        {
            "time": "20160105015723190",
            "bytes": 1668
        },
        {
            "time": "20160105015723646",
            "bytes": 7613
        }
    ],
    "nss_server": "10.56.11.186:4510",
    "end_time": "20160105015723645",
    "audio_id": 1,
    "start_time": "20160105015722835"
},
"cadence_regulatable_result": "completeRecognition",
"appserver_results": {
    "status": "success",
    "final_response": 1,
    "payload": {
        "diagnostic_info": {
            "adk_dialog_manager_status": "undefined",
            "nlu_version": "[NLU_PROJECT:NVCCP-deu-DEU];[Datapack:Version: nlps-deu-DEU-NVCCP-6.1.100.12-2-GMT20151130161021];[VL-Models:Version: vlmodels-NVCCP-deu-DEU-6.1.100.12-2-GMT20151130160231]",
            "nlps_host": "mt-dmz-nlps002.nuance.com:8636",
            "nlps_ip": "10.56.10.51",
            "application": "AUDI_2017",
            "nlu_component_flow": "[Input:VoiceJSON] [FieldID|auto_main] [NLUlib|C-eckart-r$Rev$.f20151118.1250] [build|G-r72490M.f20151130.1055] [vlmodel|Version: vlmodels-NVCCP-deu-DEU-6.1.100.12-2-GMT20151130160231] [Flow|+VlingoTokenized]",
            "third_party_delay": "0",
            "nmaid": "AUDI_SDS_2017_EXT_20151203",
            "nlps_profile": "AUDI_2017",
            "fieldId": "auto_main",
            "nlps_profile_package_version": "r159218",
            "nlu_annotator": "com.nuance.NVCCP.deu-DEU.ncs51.VlingoNLU-client-qNVCCP_NCS51",
            "ext_map_time": "3",
            "nlu_use_literal_annotator": "0",
            "int_map_time": "1",
            "nlps_nlu_type": "nlu_project",
            "nlu_language": "deu-DEU",
            "timing": {
                "finalRespSentDelay": "311",
                "intermediateRespSentDelay": "1896"
            },
            "nlps_profile_package": "AUDI_2017"
        },
        "actions": [
            {
                "Input": {
                    "Interpretations": [
                        "18 Slash 6/2015"
                    ],
                    "Type": "asr"
                },
                "Instances": [
                    {
                        "nlu_classification": {
                            "Domain": "UDE",
                            "Intention": "Unspecified"
                        },
                        "nlu_interpretation_index": 1,
                        "nlu_slot_details": {
                            "Location": {
                                "literal": "18 Slash 6/2015"
                            },
                            "Search-phrase": {
                                "literal": "18 slash 6/2015"
                            }
                        },
                        "interpretation_confidence": 3174
                    }
                ],
                "type": "nlu_results",
                "api_version": "1.0"
            }
        ],
        "nlps_version": "nlps(z):6.1.100.12.2-B359;Version: nlps-base-Zeppelin-6.1.100-B124-GMT20151130193521;"
    }
},
"final_response": 1,
"prompt": "",
"result_format": "appserver_post_results"
}

我想将上面的 JSON 转换为 python 中的字典数据类型。在上面的脚本中,我想读取这个值 -

{"Domain":"UDE","Intention":"Unspecified"}

我是json新手,所以我无法理解。有人可以给我一些建议吗?

【问题讨论】:

  • 那么你尝试了什么?还有什么你不明白的?
  • 你试过使用内置的json module吗?
  • 我试过这个:json.loads("nlu_classification")。但它不起作用

标签: python json python-2.7 python-3.x dictionary


【解决方案1】:

只需使用 python 自带的 json 模块。

import json
json_string = '{"first_name": "Guido", "last_name":"Rossum"}'
parsed_json = json.loads(json_string)
print(parsed_json['first_name'])
"Guido"

参考:http://docs.python-guide.org/en/latest/scenarios/json/

【讨论】:

  • 你能告诉我如何读取其中的域和意图值吗?
  • 非常简单。像使用任何普通字典一样使用它。
猜你喜欢
  • 2019-11-16
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 2017-08-11
  • 2018-08-31
  • 2011-06-10
  • 2012-06-14
相关资源
最近更新 更多