实现通过shell调用接口,并实现json的解析。
本案例按照两步来实现:
1、借助curl命令调用接口,上传本地json文件
2、对json文件利用python进行解析,生成结构化文本
Step1:Shell通过curl调用接口
- #!/bin/bash
- input_file=/root/workbase/id_card.txt
- output_folder=/root/workbase/curl
-
- cat $input_file | while read myline
- do
- echo $myline
- token_wb=`curl --location 'http://192.168.56.2:7070/oauth/token?client_id=xw&client_secret=2w' --header 'Content-Type: application/json' --data '{}'`
- token2=$(echo $token_wb| awk -F ':' '{print $2}' |awk -F ',' '{print $1}'| sed 's/\"//g')
- filename=`echo $myline | sed "s/[\']//g"`
- curl -X POST --location 'http://192.168.56.2:7070/service/api/q2/dataexchangeserver/apiPayload/fe2/qx?client_id=fc&access_token='$token2'' --header 'Content-Type: application/json;charset=utf-8' --header 'Accept: application/json;charset=utf-8' --data '{"messageSequence":"01","condition":"ID='$myline' and STATUS='\''有效'\''","maxReturnNum":100,"orderParafs":{"ID":"desc"}}'| cat > $output_folder/$filename.json
- ##echo $filename
- ###sleep `expr $RANDOM % 2 + 1`
- done
Step2:Python对Json进行解析
- def traverse_dir(path):
- try:
- files = glob.glob(os.path.join(path, "*"))
- for file in files:
- if os.path.isdir(file):
- print("文件夹:", file)
- traverse_dir(file)
- else:
- f = open(file, 'r', encoding='UTF8')
- data = json.load(f)
- with open('D:/household.txt', 'a', encoding='utf-8') as fw:
- for k,v in data['result'].items():
- if k == 'data':
- print(len(v['results']))
- for i in range(len(v['results'])):
- data = v['results'][i]
- NAME = data.get('NAME')
- ID = data.get('ID')
- STATUS = data.get('STATUS')
- ADDR = data.get('ADDR')
- str = NAME + '\t' + ID + '\t' + STATUS + '\t' + ADDR
- fw.write(str + '\n')
- except KeyError as e:
- print("There are missing fields in the user object: ", e)
- except FileNotFoundError as e:
- print("File is not found: ",e)
- except Exception:
- print("Error found \t", file)
- finally:
- f.close()
- fw.close()
-
-
- traverse_dir('D:\\household')
#JSON实例样例:
- {
- "code": "0",
- "message": "查询成功",
- "result": {
- "status": 200,
- "msg": "正常",
- "messageSequence": "01",
- "data": {
- "totalRows": "1",
- "results": [
- {
- "NAME": "张三",
- "ID": "234513",
- "ADDR": "北京市创新大道1000号",
- "STATUS": "有效",
- }
- ]
- },
- "qtime": 1200
- }
- }