我正在尝试解析以下JSON数据并使用python Function获取URL值。从下面的JSON示例中,我想从Jobs标记下获取URL并将其存储在2个数组中。 1个数组将存储带有颜色标签的URL,其他数组将存储没有颜色标签的URL。一旦两个数组准备就绪,我想返回这两个数组。我是python新手,需要一些帮助。
exports.myMethod2 = functions.https.onCall((data, context) => {
utilsModule.throwIfNoAuth(context);
return methodWithTransaction(context, admin);
});
exports.methodWithTransaction = function (context, admin) {
//this is called only once from outside!
return rootRef.child("my/data/path").once("value")
.then(function (indexRef) {
var usageObj = indexRef.val();
//update object's state
usageObj =
{
timestampUtcArray: [1530656089821, 1530656089822],
itemsCount: 2,
writeTimestamp: 1530656502759
}
return usageObj;
})
.then(function (usageObj) {
//her eis the sad part...
return rootRef.child("my/data/path").transaction(data => {
//this method always run 25 times and fails.
//data aways != usageObj though accross the calls usageObj are the same as well data are the same:
// usageObj =
// {
// timestampUtcArray: [1530656089821, 1530656089822],
// itemsCount: 2,
// writeTimestamp: 1530656502759 <-- only timestamp is different
// }
// data =
// {
// timestampUtcArray: [1530656089821, 1530656089822],
// itemsCount: 2,
// writeTimestamp: 1530656503421 <-- only timestamp is different
// }
return usageObj;
}
})
}
以下是我用于获取作业数据的python代码,但随后无法遍历作业数据以获取所有URL。更改代码一次只能获得1
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"actions":[ ],
"description":"This is a TSG level folder.",
"displayName":"CONSOLIDATED",
"displayNameOrNull":null,
"fullDisplayName":"CONSOLIDATED",
"fullName":"CONSOLIDATED",
"name":"CONSOLIDATED",
"url":"https://cyggm.com/job/CONSOLIDATED/",
"healthReport":[
{
"description":"Projects enabled for building: 187 of 549",
"iconClassName":"icon-health-20to39",
"iconUrl":"health-20to39.png",
"score":34
}
],
"jobs":[
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"yyfyiff",
"url":"https://tdyt.com/job/
CONSOLIDATED/job/yfiyf/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"Ops-Prod-Jobs",
"url":"https://ygduey.com/job/
CONSOLIDATED/job/Ops-Prod-Jobs/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"TEST-DATA-MGMT",
"url":"https://futfu.com/job/
CONSOLIDATED/job/TEST-DATA-MGMT/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"TESTING-OPS",
"url":"https://gfutfu.com/job/
CONSOLIDATED/job/TESTING-OPS/"
},
{
"_class":"com.cloudbees.hudson.plugins.folder.Folder",
"name":"Performance_Engineering Team",
"url":"https://ytdyt.com/job/
CONSOLIDATED/job/Performance_Engineering%20Team/"
},
{
"_class":"hudson.model.FreeStyleProject",
"name":"test",
"url":"https://tduta.com/job/
CONSOLIDATED/job/test/",
"color":"notbuilt"
}
],
"primaryView":{
"_class":"hudson.model.AllView",
"name":"all",
"url":"https://fuyfi.com/job/
CONSOLIDATED/"
},
"views":[
{
"_class":"hudson.model.AllView",
"name":"all",
"url":"https://utfufu.com/job/
CONSOLIDATED/"
}
]
}
我在这里得到第二个URL,但是无法检查此条目是否带有颜色标签
答案 0 :(得分:1)
首先,您的JSON格式不正确。您将必须使用JSON formatter来检查其有效性并解决所有问题。
也就是说,您必须使用-p作为字符串读取文件
In [87]: with open('data.json', 'r') as f:
...: data = f.read()
...:
然后使用json
库将数据加载到dict
In [88]: d = json.loads(data)
然后您可以使用2个列表推导来获取所需的数据
In [90]: no_color = [record['url'] for record in d['jobs'] if 'color' not in record]
In [91]: color = [record['url'] for record in d['jobs'] if 'color' in record]
In [93]: no_color
Out[93]:
['https://tdyt.com/job/CONSOLIDATED/job/yfiyf/',
'https://ygduey.com/job/CONSOLIDATED/job/Ops-Prod-Jobs/',
'https://futfu.com/job/CONSOLIDATED/job/TEST-DATA-MGMT/',
'https://gfutfu.com/job/CONSOLIDATED/job/TESTING-OPS/',
'https://ytdyt.com/job/CONSOLIDATED/job/Performance_Engineering%20Team/']
In [94]: color
Out[94]: ['https://tduta.com/job/CONSOLIDATED/job/test/']