简体   繁体   English

为什么json.dumps()没有返回预期的格式?

[英]Why is json.dumps() not returning the expected format?

I have a function that returns a json formatted dict . 我有一个返回json格式的dict的函数。 I am using json.dumps() to store into an object, which I print later on in the code. 我正在使用json.dumps()存储到一个对象,稍后在代码中进行打印。 What I'm getting as a returned isn't the typical json format I would have expected. 作为返回,我得到的不是我期望的典型json格式。

relevant code snippet: 相关代码段:

rasa_decoded_output = interpreter.parse(u"{}".format(textobject.body))
rasa_json_formatted_output = json.dumps(rasa_decoded_output)

printing rasa_json_formatted_output results in this (an example): 在以下示例中打印rasa_json_formatted_output结果:

("text": "This is a test", "entities":  < >, "intent_ranking":  < ("confidence": 0.71653000212560282, "name": "st_testing" ),  ("confidence": 0.05410130707298815, "name": "exists_item" ),  ("confidence": 0.024777391815713805, "name": "concierge_contact_request" ),  ("confidence": 0.020174561099034895, "name": "exists_sauna" ),  ("confidence": 0.018203983982849743, "name": "issue_bugs" ),  ("confidence": 0.017985724341235722, "name": "exists_driver" ),  ("confidence": 0.01687271448223236, "name": "request_power_adapter - custom" ),  ("confidence": 0.016857156745106013, "name": "request_newroom" ),  ("confidence": 0.015943797930370658, "name": "presto_cost" ),  ("confidence": 0.015567442054810358, "name": "exists_spa" ) >, "intent":  ("confidence": 0.07653000212560282, "name": "st_testing" ) )

What has done wrong here? 这里做错了什么? What should be tweaked to get json to return in its typical format, so that I can parse through and pull out the desired properties. 应该进行哪些调整以使json返回其典型格式,以便我可以解析并提取所需的属性。

Also, if I just print rasa_decoded_output it does the same thing as printing rasa_json_formatted_output , which suggests json.dumps() isn't having any effect. 另外,如果我只是打印rasa_decoded_output它做同样的事情印刷rasa_json_formatted_output ,这表明json.dumps()是没有任何效果。

Using json.loads() on rasa_decoded_output results in an error TypeError: the JSON object must be str, not 'dict' rasa_decoded_output上使用json.loads() rasa_decoded_output导致错误TypeError: the JSON object must be str, not 'dict'

The problem might be that rasa_decoded_output may not be a decoded json object yet . 该问题可能是rasa_decoded_output可能无法解码JSON对象 You may need to use json.loads first and once you have done so, you can use json.dumps 您可能需要先使用json.loads,一旦这样做,就可以使用json.dumps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM