Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-json加载请求参数_Python_Json - Fatal编程技术网

Python-json加载请求参数

Python-json加载请求参数,python,json,Python,Json,我在请求数据体中传递json,如下所示:- curl --data "data={"applist":{"ads":{"ad":[{"adsid":1,"view_points":25,"view_type":"full","comment":"Success"}]}}}" POSTURL 加载json时,它会抛出一个错误:- data = request.form print json.loads(str(data.get('data'))) # throws an error 在打印da

我在请求数据体中传递json,如下所示:-

curl --data "data={"applist":{"ads":{"ad":[{"adsid":1,"view_points":25,"view_type":"full","comment":"Success"}]}}}" POSTURL
加载json时,它会抛出一个错误:-

data = request.form
print json.loads(str(data.get('data'))) # throws an error

在打印data.get'data时,我得到{applist:{ads:{ad:[{adsid:1,view_points:25,view_type:full,comment:Success}]}}},这是不正确的json,因为缺少双引号。如何加载json?

问题在于通过curl的原始post请求。您使用双引号包围post数据,但也在post正文中使用双引号。最简单的修复方法是用单引号将文章正文括起来:

curl --data 'data={"applist":{"ads":{"ad":[{"adsid":1,"view_points":25,"view_type":"full","comment":"Success"}]}}}' POSTURL

首先,如果您使用的是Flask,那么应该使用request.json来获取已经解析的json。为此,您需要在curl请求中设置内容类型:

-H内容类型:应用程序/json

其次,您的数据不是有效的json。改用这个:

--data='{"applist":{"ads":{"ad":[{"adsid":1,"view_points":25,"view_type":"full","comment":"Success"}]}}}'

你在用烧瓶吗?哦。。。这是个错误。谢谢::