【发布时间】:2016-01-07 08:28:56
【问题描述】:
"Instances": [{
"nlu_classification": {
"Domain": "UDE",
"Intention": "Unspecified"
},
"nlu_interpretation_index": 1,
"nlu_slot_details": {
"Name": {
"literal": "ConnectedDrive"
},
"Search-phrase": {
"literal": "connecteddrive"
}
},
"interpretation_confidence": 5484
}],
"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"
}
我收到上述代码作为来自服务器的回复。我将这些结果存储在变量 NLU_RESULT 中。稍后我使用 json_loads 将该 json_format 转换为 dict 并检查其中的特定值,如下所示。
parsed_json = json.loads(NLU_RESULT)
print(parsed_json["Instances"]["nlu_classification"]["Domain"]).
当我使用上面的代码时。它没有打印域的值。谁能告诉我这里的错误是什么?
【问题讨论】:
-
发布的 json 不解析。
-
@suni 它不会解析,因为它(好吧,至少你在这里发布的内容)不是有效的 JSON。
-
请注意,键“Instances”的值是一个列表而不是一个字典,所以你不能像
parsed_json["Instances"]["nlu_classification"]这样访问,但这可能会工作parsed_json["Instances"][0]["nlu_classification"] -
@suni 垃圾进,垃圾出
-
我收到的错误为: Traceback(最近一次调用最后一次):文件“.\Parsing_throug_folder_DEU.py”,第 161 行,在
print(parsed_json["Instances"][0] ["nlu_classification"]) KeyError: 'Instances'
标签: python json python-2.7 python-3.x dictionary