首页 > 解决方案 > 如何将具有二进制对象的数组转换为 json 对象?

问题描述

我是 Flask 的新手。这是我在 Flask 中点击端点时得到的响应。我想将其转换为 JSON 对象。我使用 Redis DB 对用户进行排名。

[(b"{
    '_id': ObjectId('611008325498c2b76a4f6b23'), 
    'name': 'Husmitha', 
    'email': 'husmitha@abc.com', 
    'password': 'Husmitha', 
    'district': 'Colombo'
}", 25000.0),...
(b"{
    '_id': ObjectId('611014eb5498c2b76a4f6b25'), 
    'name': 'Kusal', 
    'email': 'kusal@abc.com', 
    'password': 'password', 
    'district': 'kandy'
}", 2000.0)]

这是我使用的代码。

@app.route("/getDetails", methods=["POST"])
def get_point_redis():

     earn_gain = "Earn_gained"

     user_earns = db.CropData.find()
     print(user_earns)
     for user in user_earns:
      
        user["_id"] = str(user["userID"]) 
        earnsbyUsr =  user["earns"]
        redisClient.zadd(earn_gain, {user["_id"]: earnsbyUsr})
      

     print("Contents of the Redis sorted set with scores:")
     response = redisClient.zrange(earn_gain, 0, -1, desc=True, withscores=True)

     print(response)


     for obj in response:
          bObject = user["_id"]


     return Response(
            response= json.dumps({"message": json.loads(dumps(response))}),
            status=200,

        )

我究竟做错了什么?

标签: pythonflaskredis

解决方案


推荐阅读