我从python脚本创建了json,这是我为获取json数据而编写的代码:
import requests
import json
import ConfigParser
url ="xxx"
payload ="items.find({ "repo": {"$match" : "nswps-*"}}).include("name","repo","path")
"
headers = {
'Content-Type':"text/plain",
'Authorization':"Basic xxxxxxxxx",
'Accept':"*/*",
'Cache-Control':"no-cache",
'Host':"xxxxxx.com",
'accept-encoding':"gzip, deflate",
'content-length':"77",
'Connection':"keep-alive",
'cache-control':"no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
上面的代码给了我json文件,它是一个包含多个对象的巨大文件。 由于人工制品的某些限制,我无法获得以nswps开头的存储库,但结果是所有存储库名称。 json文件具有如下数据:
"repo" :"npm-remote-cache",
"path" :"zrender/-",
"name" :"zrender-4.0.7.tgz"
},{
"repo" :"npm-remote-cache",
"path" :"ztree/-",
"name" :"ztree-3.5.24.tgz"
},{
"repo" :"nswps-docker-inprogress-local",
"path" :"ace/core/latest",
"name" :"manifest.json"
},{
"repo" :"nswps-docker-inprogress-local",
"path" :"ace/core/latest",
"name" :"sha256__0a381222a179dbaef7d1f50914549a84e922162a772ca5346b5f6147d0e5aab4"
},{
.........
现在,我需要创建一个python脚本,以提取其中只有nswps值的对象的对象,从上述json可以说,我需要这样的数据:
{
"repo" :"nswps-docker-inprogress-local",
"path" :"ace/core/latest",
"name" :"manifest.json"
},{
"repo" :"nswps-docker-inprogress-local",
"path" :"ace/core/latest",
"name" :"sha256__0a381222a179dbaef7d1f50914549a84e922162a772ca5346b5f6147d0e5aab4"
}
使用Python将字符串转换为JSON的可能重复项
resonse.json会将数据转换为Pythons列表,您可以使用for循环仅获取某些项目。
我认为它不是使用Python将字符串转换为JSON的副本。 我的问题是仅打印从nswps开始的值
Python中HTTP请求和JSON解析的可能重复项
我可以在@furas的帮助下成功完成此操作,再次感谢!
解决方案代码如下:
response = requests.request("POST", url, data=payload, headers=headers)
list = response.text
data = response.json()['results']
print("Line#24")
output_dict = [items for items in data if 'nswps' in items['repo']]
使用
data = response.json()
您应该将JSON转换为Python的列表
假设您有data
data = [
{"repo" :"npm-remote-cache",
"path" :"zrender/-",
"name" :"zrender-4.0.7.tgz"
},{
"repo" :"npm-remote-cache",
"path" :"ztree/-",
"name" :"ztree-3.5.24.tgz"
},{
"repo" :"nswps-docker-inprogress-local",
"path" :"ace/core/latest",
"name" :"manifest.json"
},{
"repo" :"nswps-docker-inprogress-local",
"path" :"ace/core/latest",
"name" :"sha256__0a381222a179dbaef7d1f50914549a84e922162a772ca5346b5f6147d0e5aab4"
}
]
那么您可以使用for循环检查列表中的每个项目并仅选择其中一些
for item in data:
if item['repo'].startswith('nswps'):
print(item)
并且作为列表理解
selected = [item for item in data if item['repo'].startswith('nswps')]
非常感谢。很高兴知道这一点。我遇到此错误,"如果item [repo] .startswith(nswps):TypeError:字符串索引必须为整数"
这意味着您的JSON具有更复杂的结构。它可以是带有字典的内部列表的外部列表。我的示例仅显示带有dictionares的列表-没有外部列表。但是您的数据可能具有更复杂的结构。开始时,使用print( item , type(item) )来查看项目中的内容。
感谢furas看着它。这是我文件的内容(只是一个片段){urange:{utotal:123812,uend_pos:123812,ustart_pos:0},结果:[{urepo:ujcenter-cache,upath:u" com / virtualightning / stateframework / 0.1。 1",uname:ustateframework-0.1.1-sources.jar},{urepo:ujcenter-cache,upath:u" com / virtualightning / stateframework / 0.1.1",uname:ustateframework-0.1.1.aar},{ urepo:ujcenter-cache,正常运行:u。,uname:usdk-release},{urepo:ujcenter-cache,正常运行:uabbot / abbot / 0.12.3,uname:uabbot-0.12.3.jar}
您是否使用response.json()? string indices must be integers"可能意味着您使用了response.text。使用代码段可以是data = response.json()[results]
非常感谢您的帮助,我可以完成任务。总而言之,我的输入是我使用rest API从工件获得的json。然后,通过仅选择所需内容来减小文件的大小。这是代码:response = request.request(" POST",url,data = payload,headers = headers)list = response.text output_dict = [如果nswps in items [repo],则为数据中的项目]再次感谢对我有很大帮助!