andylin02 发表于 2013-1-26 14:31:09

python 解析 json

#!/usr/bin/env pythonimport sys;import simplejson as json;def GetJsonVal(strJsonFile, lsKey):    try:      if len(lsKey) <= 0:            return [-1, ''];                mapJson = json.load(file(strJsonFile));      mapVal = mapJson;      for strKey in lsKey:            if not mapVal.has_key(strKey):                return [-2, ''];            else:                mapVal = mapVal;      return ;               except:      return [-10, ''];def usage():    strExe = sys.argv;    print("Usage: %s json_file json_path\nExample:%s/a.json a/b/c" %(strExe, strExe))    if "__main__" == __name__:    strJson = sys.argv;    lsKey = sys.argv;    lsRet = GetJsonVal(strJson, lsKey);    if 0 != lsRet:      print("get key:%s failed! error code:%s" %('/'.join(lsKey), lsRet));      sys.exit(lsRet);    else:      print(lsRet);      sys.exit(0);
页: [1]
查看完整版本: python 解析 json