最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask中如何解析已得到的JSON字符串中的参数

Flask crifan 4718浏览 0评论

之前折腾过Flask-restful中,利用:

reqparse.RequestParser()

和add_argument

然后去实现参数解析。

现在(通过websocket)得到一个json字符串,比如:

{
     "longitude" : 31.2639030000,
     "latitude" : 120.7297170000,
     "shortStr" : "星湖街134号",
     "fullStr" : "江苏省苏州市吴中区星湖街134号"
}

想要看看能否借用RequestParser去解析出对应的参数

flask parse parameter from json

python – flask – how to get parameters from a JSON GET request – Stack Overflow

http://stackoverflow.com/questions/26069714/flask-how-to-get-parameters-from-a-json-get-request

Parser attempts to parse json arguments for GET requests · Issue #148 · flask-restful/flask-restful

19.2. json — JSON encoder and decoder — Python 3.5.2 documentation

【总结】

最后还是自己去使用json转换为dict变量,自己实现参数的解析了:

def strToJson(jsonStr):
    jsonDict = json.loads(jsonStr, encoding="utf-8")
    gLog.debug("jsonStr=%s, jsonDict=%s", jsonStr, jsonDict)
    return jsonDict
def wsParseMessage(messageStr):
    messageDict = strToJson(messageStr)
    gLog.debug("messageStr=%s, messageDict=%s", messageStr, messageDict)
    (parseOk, msgDictOrFailReason) = (False, "Unknown reason")
    if ("type" in messageDict) and \
        ("id" in messageDict) and \
        ("event" in messageDict) and \
        ("data" in messageDict) :
        parseOk = True
        msgDictOrFailReason = messageDict
    else:
        parseOk = False
        msgDictOrFailReason = "Invalid message format"
    gLog.debug("parseOk=%s, msgDictOrFailReason=%s", parseOk, msgDictOrFailReason)
    return (parseOk, msgDictOrFailReason)
def wsProcessMessage(messageDict):
    gLog.debug("messageDict=%s", messageDict)
    msgType = messageDict["type"]
    msgId = messageDict["id"]
    msgEvent = messageDict["event"]
    msgData = messageDict["data"]
    gLog.debug("msgType=%s, msgId=%s, msgEvent=%s, msgData=%s", msgType, msgId, msgEvent, msgData)
    gLog.debug("type(msgData)=%s", type(msgData))
    if msgType == "User":
        userId = msgId
        gLog.debug("userId=%s", userId)
        curUser = User.query.filter_by(id=userId).first()
        gLog.debug(‘curUser=%s’, curUser)
        if curUser is None:
            return (False, "invalid user id %s"%(userId))
        if msgEvent == "SaveLocation":
            locationDataDict = msgData
            gLog.debug("locationDataDict=%s", locationDataDict)
            if ("longitude" in locationDataDict) and \
                    ("latitude" in locationDataDict) and \
                    ("shortStr" in locationDataDict) and \
                    ("fullStr" in locationDataDict):
                gLog.debug("old location curUser.location=%s", curUser.location)
                curUser.location.longitude = locationDataDict["longitude"]
                curUser.location.latitude = locationDataDict["latitude"]
                curUser.location.shortStr = locationDataDict["shortStr"]
                curUser.location.fullStr = locationDataDict["fullStr"]
                db.session.commit()
                gLog.debug("new location curUser.location=%s", curUser.location)
                return (True, "save user %s new location ok"%(userId))
            else:
                return (False, "Invalid location format for %s"%(locationDataDict))
        else:
            return (False, "Unsupport user %s event %s"%(userId, msgEvent))
    else:
        return (False, "Unsupport type %s" % (msgType))
            (isValidMessage, messageDictOrFailReason) = wsParseMessage(unicodeMsg)
            gLog.debug("isValidMessage=%s, messageDictOrFailReason=%s", isValidMessage, messageDictOrFailReason)
            if isValidMessage:
                (processOk, respMsgOrFailReason) = wsProcessMessage(messageDict=messageDictOrFailReason)
                gLog.debug("processOk=%s, respMsgOrFailReason=%s", processOk, respMsgOrFailReason)

转载请注明:在路上 » 【已解决】Flask中如何解析已得到的JSON字符串中的参数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.187 seconds, using 22.07MB memory