来自API的Python #数据
plan_get = pd.DataFrame(rows, columns=columns) #plan_get return all json data
return Response({"MESSAGE": "FOUND","DATA":json.loads(plan_get.to_json(orient='records'))})
实际产量:
[{
"customer_name": "ABI2",
"location_name": "Cherai2",
"employee_name": "ASU2",
"Sales_Plan_Details": "[{\"Month\": \"2019-1\", \"Quantity\": 10, \"Product_Gid\": 3}]"
},
{
"customer_name": "ABI",
"location_name": "Cherai",
"employee_name": "ASU",
"Sales_Plan_Details": "[{\"Month\": \"2019-1\", \"Quantity\": 10, \"Product_Gid\": 3}]"
}]
预期产出:
[{
"customer_name": "ABI2",
"location_name": "Cherai2",
"employee_name": "ASU2",
"Sales_Plan_Details": [{"Month": "2019-1",
"Quantity": 10, "Product_Gid": 3}]
},
{
"customer_name": "ABI",
"location_name": "Cherai",
"employee_name": "ASU",
"Sales_Plan_Details": [{"Month": "2019-1",
"Quantity": 10, "Product_Gid": 3}]
}]
这里我使用PandasDataFrame来传递JSON数据,我的问题是如何在返回之前将Sales_Plan_Details(column)
转换为JSON对象。
2条答案
按热度按时间0ejtzxu11#
使用
json.loads
或ast.literal_eval
将string
转换为list of dicts
:设置:
zzlelutf2#
您可以使用list comprehensionsMap
Sales_Plan_Details
值。可以使用json.loads()从字符串反序列化列表值。