jsonp跨域请求响应结果处理函数(python)

接口测试跨域请求接口用的jsonp,需要将回调函数里的json字符串提取出来。

jsonp跨域请求的响应结果格式: callback_functionname(json字符串)。

#coding:utf-8

import json

def jsonp_to_json(jsonp_string):
    """get jsonstr from jsonpstr=callback(jsonstr)"""
    left = jsonp_string.find('(') #找到第一个(的索引
    json_string = json.loads(jsonp_string[left+1:-1])  # 取callback()括号里的内容
    return json_string

the end!

原文地址:https://www.cnblogs.com/dinghanhua/p/10246533.html