Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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/4/json/13.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从CURL输出解析JSON_Python_Json_Curl_Python Requests - Fatal编程技术网

使用python从CURL输出解析JSON

使用python从CURL输出解析JSON,python,json,curl,python-requests,Python,Json,Curl,Python Requests,我试图获取id的值,即id:59,它以json的形式出现在curl输出中。 以下是json中的curl输出: [{"id":59,"description":"This is a demo project","name":"Demo_Project","name_with_namespace":"sam / Demo_Project","path":"demo_project","path_with_namespace":"sam/demo_project","created_at":"2020

我试图获取id的值,即id:59,它以json的形式出现在curl输出中。 以下是json中的curl输出:

[{"id":59,"description":"This is a demo project","name":"Demo_Project","name_with_namespace":"sam / Demo_Project","path":"demo_project","path_with_namespace":"sam/demo_project","created_at":"2020-03-02T08:43:13.664Z","default_branch":"master","tag_list":[],"ssh_url_to_repo":"ssh://git@od-test.od.com:2222/sam/demo_project.git","http_url_to_repo":"https://od-test.od.com/gitlab/sam/demo_project.git","web_url":"https://od-test.od.com/gitlab/sam/demo_project","readme_url":"https://od-test.od.com/gitlab/sam/demo_project/blob/master/README.md","avatar_url":null,"star_count":0,"forks_count":0,"last_activity_at":"2020-04-09T09:28:09.860Z","namespace":{"id":2259,"name":"sam","path":"sam","kind":"user","full_path":"sam","parent_id":null,"avatar_url":"https://secure.gravatar.com/avatar/755db8ssqaq50dcc9d189c53523b?s=80\u0026d=identicon","web_url":"https://od-test.od.com/gitlab/sam"}}]
我正在使用python解析json并获取id的值。 我已尝试使用以下命令执行相同操作,但出现错误

curl --header "PRIVATE-TOKEN: 9999ayayayar66" "https://od-test.od.com/gitlab/api/v4/search?scope=projects&search=demo_project" | python -c 'import sys, json; print(json.load(sys.stdin)["id"])'
错误:

有人能帮我使用正确的python命令来获取id的值吗。
提前感谢。

JSON包含一个对象数组,但您将其视为单个对象:

import sys, json; print(json.load(sys.stdin)["id"])
你基本上是说这里是一个对象的集合,给我对象的ID。这没有道理

如果假设只需要数组中第一个对象的ID,可以使用以下方法:

import sys, json; print(json.load(sys.stdin)[0]["id"])

您必须编写printjson.loadsys.stdin[0][id],因为json响应是一个字典列表,而不是一个字典。

json对象的根实际上是一个数组,因此您应该首先从中获取第一项:

curl --header "PRIVATE-TOKEN: 9999ayayayar66" "https://od-test.od.com/gitlab/api/v4/search?scope=projects&search=demo_project" | python -c 'import sys, json; print(json.load(sys.stdin)[0]["id"])'
由于还添加了标记,因此CURL命令可以转换为:

import requests

headers = {
    'PRIVATE-TOKEN': '9999ayayayar66',
}

params = (
    ('scope', 'projects'),
    ('search', 'demo_project'),
)

response = requests.get('https://od-test.od.com/gitlab/api/v4/search', headers=headers, params=params)

然后您可以使用response.json[0][id]从中获取id值。这使用内置的JSON解码器将JSON响应转换为字典列表

聪明的问题,你几乎自己解决了。我以前从未想过管道系统。谢谢你。欢迎@PraysonW.Daniel