我有一个JSON文件,其格式是:对象数组。每个对象代表一个类。每个JSON对象都有一个“方法”键,代表所分析类的所有方法。格式很简单,如下面的示例所示 这不是完整的JSON文件,它是一个类的块
[
{
"methods": [
{
"parametersTypes": [
"String"
],
"metricsValues": {
"ParameterCount": 1.0,
"CyclomaticComplexity": 1.0,
"LocalityRatio": 1.0,
"MethodLinesOfCode": 2.0,
"MaxCallChain": 1.0
},
"fullyQualifiedName": "Smiley.Smiley",
"smells": []
},
{
"parametersTypes": [
"String"
],
"metricsValues": {
"ParameterCount": 1.0,
"CyclomaticComplexity": 8.0,
"LocalityRatio": null,
"MethodLinesOfCode": 23.0,
"MaxCallChain": 0.0
},
"fullyQualifiedName": "Smiley.checkSmiley",
"smells": [
{
"name": "LazyClass",
"reason": null,
"startingLine": 3,
"endingLine": 36
}
]
}
],
"metricsValues": {
"PublicFieldCount": 0.0,
"TightClassCohesion": 0.0,
"IsAbstract": 0.0,
"ClassLinesOfCode": 27.0,
"OverrideRatio": 0.037037037037037035
},
"fullyQualifiedName": "Smiley",
"smells": [
{
"name": "LazyClass",
"reason": null,
"startingLine": 10,
"endingLine": 70
}
]
}
]
我需要收集JSON文件上的所有气味,然后我会将它们放入DataFrame中 我上一堂课 这是我的代码
import json
from pprint import pprint
with open('Testan.json') as f:
data = json.load(f)
pprint(data)
for datum in data[0]["methods"]:
print(datum['smells'])
AND THIs是结果:
[]
[{'name': 'LazyClass', 'reason': 'null', 'startingLine': 3, 'endingLine': 36}]
[{'name': 'LazyClass', 'reason': 'null', 'startingLine': 10, 'endingLine': 70}]
首先,我需要从列表中获取名称(LazyClass),然后从所有类JSON File而不是从第一类收集数据。
请提供任何帮助,并在此先感谢!
答案 0 :(得分:0)
使用下面的代码,您可以将df
列表中包含的所有类别的所有气味添加。所有数据都存储在df['name']
数据框中。您可以通过your_classes = ['Testan.json','other_names.json']
your_smells = []
for filename in your_classes:
with open(filename) as f:
data = json.load(f)
for datum in data[0]["methods"]:
your_smells.append(datum['smells'])
import pandas as pd
df = pd.DataFrame(your_smells) # Dataframe with all the smells
print(list(df['name'])) # The list of all names from all classes
.vue