TypeError:JSON对象必须为str,而不是'DetailedResponse'

时间:2019-05-23 16:28:18

标签: python django string postgresql jupyter-notebook

我已经建立了一个Jupyter Notebook来连接我的Postgresql database,调用表中的数据,并将API中的机器学习模型应用于这些数据,但是我不断得到一个{{1} }。

我的笔记本电脑分为3个单元格,但为了清楚起见,我将它们放在一起:

TypeError: the JSON object must be str, not 'DetailedResponse'

以及完整的错误输出:

from watson_developer_cloud import NaturalLanguageClassifierV1
import pandas as pd
import psycopg2
import json

conn_string = 'host={} port={}  dbname={}  user={}  password={}'.format('119.203.10.242', 5432, 'mydb', 'locq', 'Mypass***')
conn_cbedce9523454e8e9fd3fb55d4c1a52e = psycopg2.connect(conn_string)

data_df_1 = pd.read_sql('SELECT description from public."search_product"', con=conn_cbedce9523454e8e9fd3fb55d4c1a52e)


natural_language_classifier = NaturalLanguageClassifierV1(
    iam_apikey='F76ugy8hv1s3sr87buhb7564vb7************'
)


classes = natural_language_classifier.classify('7818d2s519-nlc-1311', data_df_1.to_json())

print(json.loads(classes, indent=2))

我已经尝试了很多方法来达到目标​​,但是现在我完全陷入了困境,找不到解决此问题的方法,或者我可能只是不太了解解决问题的代码。

请帮助。

编辑

API在输出中生成标头,因此在尝试序列化时出现错误,有没有办法删除标头?

输出:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-b6b7d122a500> in <module>()
      2 
      3 classes = natural_language_classifier.classify('7818d2s519-nlc-1311', data_df_1.to_json())
----> 4 print(json.loads(classes, indent=2))

/opt/conda/envs/DSX-Python35/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    310     if not isinstance(s, str):
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):
    314         raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",


TypeError: the JSON object must be str, not 'DetailedResponse'

2 个答案:

答案 0 :(得分:2)

我不熟悉这个图书馆,但我会尽力解决您的问题。

from watson_developer_cloud import NaturalLanguageClassifierV1
import pandas as pd
import psycopg2
import json

conn_string = 'host={} port={}  dbname={}  user={}  password={}'.format('119.203.10.242', 5432, 'mydb', 'locq', 'Mypass***')
conn_cbedce9523454e8e9fd3fb55d4c1a52e = psycopg2.connect(conn_string)

data_df_1 = pd.read_sql('SELECT description from public."search_product"', con=conn_cbedce9523454e8e9fd3fb55d4c1a52e)


natural_language_classifier = NaturalLanguageClassifierV1(
    iam_apikey='F76ugy8hv1s3sr87buhb7564vb7************'
)


classes = natural_language_classifier.classify('7818d2s519-nlc-1311', data_df_1.to_json())

data_dict = classes._to_dict() # this will returns dict
data_dict.pop('headers', None) # remove headers key
print(data_dict) # This shows you dictionary in terminal
# if you want to serialize this and save on disk, you should make json serialization for example.
json_data = json.dumps(data_dict)
print(json_data) # this shows your string(json) in terminal

也尝试这种方式。

答案 1 :(得分:1)

我要添加评论,但我没有声誉

json.loads()需要一个字符串,并且看来您的classes变量不是字符串。您可能需要执行str(classes)才能使用它的字符串表示形式