Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

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

$
0
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)


Viewing all articles
Browse latest Browse all 9596

Trending Articles