rpy2: json.dumps() 的输出确实不是正确的 json 对象
rpy2: output of json.dumps() is really not proper json object
在我的后端代码中,我使用 rpy2
从 python 调用了 R 函数,并且我的 R 函数有字典输出,我需要将其呈现为 JSON
在我的服务器端点。但是我的字典 json.dumps()
的输出实际上不是 JSON
对象。我想通了,因为 R 函数的输出为 StrVector
,我必须在字典中对输出数据进行字符串化,然后使用 json.dumps
,但仍然无法获得正确格式的 JSON。
为什么 json.dumps
对字典中的数据表现得很奇怪?在端点获取正确 JSON 结构化数据的正确方法是什么?有什么想法吗?
在字典中输入数据:
这里是R函数返回的字典中的数据。这本词典的类型是 <class 'rpy2.robjects.vectors.StrVector'>
mydict = {"score":[72.188,62.0955,19.3374],"category":"medium",
"guidance":"text description","readiness":true,
"features_used":{"name":"gcstotal","value":null,
"range_value":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
"range_frequency":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],"importance":0}}
更新:
我刚刚对 mydict
进行了质量检查,它是 <class 'rpy2.robjects.vectors.StrVector'>
,所以我在我的代码中尝试了以下:
json.dumps(str(mydict))
然后我最终低于输出。我从 python 调用了 R 函数,并且我使用 rpy2
这样做,但是 StrVector 的 json.dumps()
的输出并不是真正的 json。为什么?有什么想法吗?
当我将这本词典提供给 json.dumps(mydict )
时,我得到了这个输出:
"{\"score\":[72.188,62.0955,19.3374],\"category\":\"medium\",
\"guidance\":\"text description\",\"readiness_flag\":true,
\"features_used\":{\"name\":\"gcstotal\",\"value\":null,
\"range_value\":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
\"range_frequency\":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],\"importance\":0}} \n"
我只是不明白为什么最终得到来自 json.dumps
的非 JSON 对象。是 json.dumps
的问题还是我在服务器端点对 jsonify 字典使用了错误的方式?为什么我有以上输出?任何的想法?谢谢
更新 2::
在我尝试@Victor S 解决方案后,它适用于粘贴的字典,但我不确定我是否可以按如下方式装饰 mydict 对象:
def post(self):
if not request.get_json():
return bad_request('No input data provided')
raw_dict = request.json
input_json = json.dumps(raw_dict)
mydict = my_R_func(input_json)
mydict = """mydict""" ## inspired from @Victor S
res = json.loads(str(mydict))
res = json.dumps(res)
return jsonify(res)
我可以装饰mydict = my_R_func(input_json); mydict = """ mydict """ ?有什么办法吗?
尝试
dict(zip(mydict.names, map(list, list(mydict))))
感谢@CT Zhu this answer
显然这不会处理嵌套列表,因此您也可以使用 rpy2(pip install rpy2):
from rpy2.robjects import pandas2ri
pandas2ri.activate()
pandas2ri.ri2py(mydict)
Out[294]:
{"score":[72.188,62.0955,19.3374],"category":"medium",
"guidance":"text description","readiness":true,
"features_used":{"name":"gcstotal","value":null,
"range_value":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
"range_frequency":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],"importance":0}}
当您调用 json.dumps() 时,您需要向其传递一个有效的 Python 字典。例如,您的 "readiness":true true 会抛出错误,因为它不是有效的 Python 布尔变量,而是 JSON 语法。
尝试以下方法解决此问题。
import json
mydict = """{"score":[72.188,62.0955,19.3374],"category":"medium",
"guidance":"text description","readiness":true,
"features_used":{"name":"gcstotal","value":null,
"range_value":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
"range_frequency":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],"importance":0}}"""
# convert to string
mydict_str = json.loads(mydict)
# to json obj.
tojson = json.dumps(mydict_str , indent=4)
print(tojson)
在我的后端代码中,我使用 rpy2
从 python 调用了 R 函数,并且我的 R 函数有字典输出,我需要将其呈现为 JSON
在我的服务器端点。但是我的字典 json.dumps()
的输出实际上不是 JSON
对象。我想通了,因为 R 函数的输出为 StrVector
,我必须在字典中对输出数据进行字符串化,然后使用 json.dumps
,但仍然无法获得正确格式的 JSON。
为什么 json.dumps
对字典中的数据表现得很奇怪?在端点获取正确 JSON 结构化数据的正确方法是什么?有什么想法吗?
在字典中输入数据:
这里是R函数返回的字典中的数据。这本词典的类型是 <class 'rpy2.robjects.vectors.StrVector'>
mydict = {"score":[72.188,62.0955,19.3374],"category":"medium",
"guidance":"text description","readiness":true,
"features_used":{"name":"gcstotal","value":null,
"range_value":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
"range_frequency":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],"importance":0}}
更新:
我刚刚对 mydict
进行了质量检查,它是 <class 'rpy2.robjects.vectors.StrVector'>
,所以我在我的代码中尝试了以下:
json.dumps(str(mydict))
然后我最终低于输出。我从 python 调用了 R 函数,并且我使用 rpy2
这样做,但是 StrVector 的 json.dumps()
的输出并不是真正的 json。为什么?有什么想法吗?
当我将这本词典提供给 json.dumps(mydict )
时,我得到了这个输出:
"{\"score\":[72.188,62.0955,19.3374],\"category\":\"medium\",
\"guidance\":\"text description\",\"readiness_flag\":true,
\"features_used\":{\"name\":\"gcstotal\",\"value\":null,
\"range_value\":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
\"range_frequency\":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],\"importance\":0}} \n"
我只是不明白为什么最终得到来自 json.dumps
的非 JSON 对象。是 json.dumps
的问题还是我在服务器端点对 jsonify 字典使用了错误的方式?为什么我有以上输出?任何的想法?谢谢
更新 2::
在我尝试@Victor S 解决方案后,它适用于粘贴的字典,但我不确定我是否可以按如下方式装饰 mydict 对象:
def post(self):
if not request.get_json():
return bad_request('No input data provided')
raw_dict = request.json
input_json = json.dumps(raw_dict)
mydict = my_R_func(input_json)
mydict = """mydict""" ## inspired from @Victor S
res = json.loads(str(mydict))
res = json.dumps(res)
return jsonify(res)
我可以装饰mydict = my_R_func(input_json); mydict = """ mydict """ ?有什么办法吗?
尝试
dict(zip(mydict.names, map(list, list(mydict))))
感谢@CT Zhu this answer
显然这不会处理嵌套列表,因此您也可以使用 rpy2(pip install rpy2):
from rpy2.robjects import pandas2ri
pandas2ri.activate()
pandas2ri.ri2py(mydict)
Out[294]:
{"score":[72.188,62.0955,19.3374],"category":"medium",
"guidance":"text description","readiness":true,
"features_used":{"name":"gcstotal","value":null,
"range_value":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
"range_frequency":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],"importance":0}}
当您调用 json.dumps() 时,您需要向其传递一个有效的 Python 字典。例如,您的 "readiness":true true 会抛出错误,因为它不是有效的 Python 布尔变量,而是 JSON 语法。
尝试以下方法解决此问题。
import json
mydict = """{"score":[72.188,62.0955,19.3374],"category":"medium",
"guidance":"text description","readiness":true,
"features_used":{"name":"gcstotal","value":null,
"range_value":[3.6667,5,6.3333,7.6667,9,10.3333,11.6667,13,14.3333],
"range_frequency":[0.0024,0,0.0049,0.0016,0.0073,0.0195,0.0098,0.0138,0.9406],"importance":0}}"""
# convert to string
mydict_str = json.loads(mydict)
# to json obj.
tojson = json.dumps(mydict_str , indent=4)
print(tojson)