廢話不多說(shuō),以下代碼可以用于截取Json字符串中任意字段名的值
需要先使用pip安裝 re
import re
class jsonUtils:
? ? #get the value of key
? ? def getValueByKey(jsonStr,key):
? ? ? ? keyStartIndex = jsonStr.find(key)
? ? ? ? jsonStr = jsonStr[keyStartIndex+len(key):len(jsonStr)]? ? ? # cut the String from key index
? ? ? ? jsonstrs = re.split("['|\"]",jsonStr)
? ? ? ? if len(jsonstrs)<2:
? ? ? ? ? ? return None
? ? ? ? else:
? ? ? ? ? ? return jsonstrs[2]