黑盒测试 发表于 2016-12-26 12:26:11

【求助】python接口返回数据转json格式

python接口返回数据转json格式
一、
req=requests.get('http://huoxiutest.51tv.com/cmsServlet',headers=headers,params=params,cookies=cookies,timeout=1000)
正确的可以打印出来print(req.text)
print(req.json())
报错json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
二、
req=requests.get('http://huoxiutest.51tv.com/cmsServlet',headers=headers,params=params,cookies=cookies,timeout=1000)
req2=json.loads(req.text)
print(req2)
报错:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
三、
req=requests.get('http://huoxiutest.51tv.com/cmsServlet',headers=headers,params=params,cookies=cookies,timeout=1000)
req2=json.dumps(req)
print(req2)
报错:TypeError: <Response > is not JSON serializable

黑盒测试 发表于 2016-12-26 12:38:37

jmeter结果是这样的是不是格式不对导致
要用正则提取吗?如何去做呢?



菜鸟@大虾 发表于 2016-12-26 14:06:30

   req.json()报错是因为没有有效的json对象。我个人猜测你是想输出响应报文的body(如果我猜对了,那么正确的方法是用req.text或者req.content)

黑盒测试 发表于 2016-12-26 14:17:20

菜鸟@大虾 发表于 2016-12-26 14:06
   req.json()报错是因为没有有效的json对象。我个人猜测你是想输出响应报文的body(如果我猜对了 ...

是的req.text是可以正常输出来但是内容太多不太好看
我想转成json格式 目前不知怎么转

菜鸟@大虾 发表于 2016-12-26 14:46:19

黑盒测试 发表于 2016-12-26 14:17
是的req.text是可以正常输出来但是内容太多不太好看
我想转成json格式 目前不知怎么转

兄弟,你把这个响应的text贴出来下 我这边试试,贴文字哦 不要图片 嘿嘿

黑盒测试 发表于 2016-12-26 15:03:13

菜鸟@大虾 发表于 2016-12-26 14:46
兄弟,你把这个响应的text贴出来下 我这边试试,贴文字哦 不要图片 嘿嘿

jQuery1113024146582369876446_1481799104520({"end":false,"array":[{"id":"58","description":"小编新增","title":"小编新增","link":"17","image":"http://hximages.51tv.com/article/2016-12-23/1482485811867.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-23/1482485872547.jpg","updateDate":"5天前","url":"news/58.html","ifsize":1,"format":0,"username":"普通编辑","faceimage":""},{"id":"5","description":"","title":"吴敏霞宣布退役!将在水立方举行退役仪式","link":"32","image":"http://hximages.51tv.com/article/2016-12-15/1481782872582.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-15/1481782866052.jpg","updateDate":"2016-12-15","url":"news/5.html","ifsize":0,"format":0,"username":"汪晓瑜","faceimage":"http://hximages.51tv.com/sysUser/2016-12-20/1482202331102.jpg"},{"id":"47","description":"燃爆这个冬天!","title":"有了蒋瑶佳这针鸡血,大学生何愁不过期末考试周!","link":"120","image":"http://hximages.51tv.com/article/2016-12-19/1482161070305.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-19/1482161085570.jpg","updateDate":"6天前","url":"news/47.html","ifsize":0,"format":0,"username":"汪晓瑜","faceimage":"http://hximages.51tv.com/sysUser/2016-12-20/1482202331102.jpg"},{"id":"22","description":"我知道你知道","title":"12月14日,前台湾&ldquo;文化部长&rdquo;龙应台女士在香港大学演讲,演讲主题为&ldquo;一首歌,一个时代&rdquo;。 当龙应台问到台下观众,他们的&ldquo;人生启蒙歌是什么&rdquo;的时候,一个教授模样的中年男子用夹杂着港腔的普通话说道:&ldquo;我记得那时刚到学校的时候,很多师兄都带着我们唱&lsquo;我的祖国&rsquo;&hellip;&hellip;&rdquo;","link":"211","image":"http://hximages.51tv.com/article/2016-12-16/1481855122844.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-16/1481855126241.jpg","updateDate":"10天前","url":"news/22.html","ifsize":0,"format":0,"username":"系统管理员","faceimage":"http://hximages.51tv.com/sysUser/2016-12-19/1482129297269.gif"},{"id":"62","description":"听到&ldquo;一条大河波浪宽&rdquo;的时候,龙应台女士还算蛮镇定的&hellip;&hellip;","title":"&ldquo;我的祖国&rdquo;,港大师生回答龙应台什么是中国人的启蒙歌曲!","link":"9","image":"http://hximages.51tv.com/article/2016-12-22/1482375672812.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-22/1482375678443.jpg","updateDate":"4天前","url":"news/62.html","ifsize":1,"format":1,"username":"主编","faceimage":"http://hximages.51tv.com/sysUser/2016-12-19/1482127864094.png"},{"id":"55","description":"居中","title":"上海人冬天离不开的美食,看到第一个就忍不住了!","link":"9","image":"http://hximages.51tv.com/article/2016-12-22/1482388887291.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-22/1482388895504.jpg","updateDate":"5天前","url":"news/55.html","ifsize":0,"format":0,"username":"普通编辑","faceimage":""},{"id":"4","description":"234578978675656","title":"女子两万现金被抢 穿高跟鞋狂追半小时终擒贼","link":"62","image":"http://hximages.51tv.com/article/2016-12-15/1481782399813.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-15/1481782411508.jpg","updateDate":"2016-12-15","url":"news/4.html","ifsize":0,"format":0,"username":"汪晓瑜","faceimage":"http://hximages.51tv.com/sysUser/2016-12-20/1482202331102.jpg"},{"id":"48","description":"11111","title":"11111","link":"12","image":"http://hximages.51tv.com/article/2016-12-20/1482205231216.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-20/1482205220704.jpg","updateDate":"6天前","url":"news/48.html","ifsize":0,"format":0,"username":"汪晓瑜","faceimage":"http://hximages.51tv.com/sysUser/2016-12-20/1482202331102.jpg"},{"id":"60","description":"就问你怕不怕!","title":"BBF男团首张专辑发布 《王者之路》霸气登录人大会堂","link":"52","image":"http://hximages.51tv.com/article/2016-12-21/1482288880453.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-21/1482288887993.jpg","updateDate":"5天前","url":"news/60.html","ifsize":1,"format":1,"username":"汪晓瑜","faceimage":"http://hximages.51tv.com/sysUser/2016-12-20/1482202331102.jpg"}],"status":"success","errorCode":""})

黑盒测试 发表于 2016-12-26 15:24:59

本帖最后由 黑盒测试 于 2016-12-26 15:31 编辑

菜鸟@大虾 发表于 2016-12-26 14:46
兄弟,你把这个响应的text贴出来下 我这边试试,贴文字哦 不要图片 嘿嘿回复你了你看下

黑盒测试 发表于 2016-12-26 17:35:34

黑盒测试 发表于 2016-12-26 15:03
jQuery1113024146582369876446_1481799104520({"end":false,"array":[{"id":"58","description":"小编新 ...

jQuery1113024146582369876446_1481799104520({"end":false,"array":[{"id":"58","description":"小编新增","title":"小编新增","link":"17","image":"http://hximages.51tv.com/article/2016-12-23/1482485811867.jpg","bigimage":"http://hximages.51tv.com/article/2016-12-23/1482485872547.jpg","updateDate":"5天前","url":"news/58.html","ifsize":1,"format":0,"username":"普通编辑","faceimage":""},




去掉这个参数,可以正常res.json()出来,不过显示格式还是太乱了,估计需要下载个json插件才能显示不叫明确,好查看
‘callback’:‘jQuery1113024146582369876446_1481799104520’

菜鸟@大虾 发表于 2016-12-26 17:42:00

你使用demjson库(pip install demjson后import即可),我这边效果如图

黑盒测试 发表于 2016-12-26 17:56:41

菜鸟@大虾 发表于 2016-12-26 17:42
你使用demjson库(pip install demjson后import即可),我这边效果如图

好的我目前查看还是dict类型我到如下库 试试

黑盒测试 发表于 2016-12-26 18:10:13

菜鸟@大虾 发表于 2016-12-26 17:42
你使用demjson库(pip install demjson后import即可),我这边效果如图

我的安装前和安装后都是dict格式
字典格式就是json格式吗?

菜鸟@大虾 发表于 2016-12-26 18:48:48

黑盒测试 发表于 2016-12-26 18:10
我的安装前和安装后都是dict格式
字典格式就是json格式吗?

Python里的dict字典类型 就是json格式:)

黑盒测试 发表于 2016-12-26 19:37:43

菜鸟@大虾 发表于 2016-12-26 18:48
Python里的dict字典类型 就是json格式

那就对了我装demjson之前和之后一样那就对着呢
页: [1]
查看完整版本: 【求助】python接口返回数据转json格式