有人可以帮我从下面的json中获取数据吗?我有下面格式的json数据
[
{
"methods": [
{
"parametersTypes": [
"int",
"Menu"
],
"sourceFile": {
"file": {
"path": "/mnt/c/anyplace-master/android/actionbarsherlock/src/android/support/v4/app/Watson.java"
}
},
"metricsValues": {
"MethodLinesOfCode": 33.0,
"CyclomaticComplexity": 13.0,
"CouplingDispersion": 0.6666666666666666,
"MaxNesting": 5.0,
"ChangingClasses": 0.0,
"ParameterCount": 2.0,
"CouplingIntensity": 3.0,
"NumberOfAccessedVariables": 13.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0
},
"fullyQualifiedName": "android.support.v4.app.Watson.onCreatePanelMenu",
"smells": [
{
"name": "LongMethod",
"reason": "MLOC > 6.74646840148693",
"startingLine": 39,
"endingLine": 82
}
]
我只需要键“ name”的值,即“ longMethod”
"name": "LongMethod"
注意:此块是JSON文件的一部分,重复
提前谢谢!
答案 0 :(得分:0)
您可以使用json
打开文件:
import json
with open('your_file.json') as f:
data = json.load(f)
并使用:
访问变量data[0]['methods']['smells'][0]['name']
答案 1 :(得分:0)
假设我对不完整json的有根据的猜测是正确的,这就是您要做的事
s="""[
{
"methods":
[
{
"parametersTypes":
[
"int",
"Menu"
],
"sourceFile":
{
"file":
{
"path": "/mnt/c/anyplace-master/android/actionbarsherlock/src/android/support/v4/app/Watson.java"
}
},
"metricsValues":
{
"MethodLinesOfCode": 33.0,
"CyclomaticComplexity": 13.0,
"CouplingDispersion": 0.6666666666666666,
"MaxNesting": 5.0,
"ChangingClasses": 0.0,
"ParameterCount": 2.0,
"CouplingIntensity": 3.0,
"NumberOfAccessedVariables": 13.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0
},
"fullyQualifiedName": "android.support.v4.app.Watson.onCreatePanelMenu",
"smells":
[
{
"name": "LongMethod",
"reason": "MLOC > 6.74646840148693",
"startingLine": 39,
"endingLine": 82
}
]
}
]
}
]"""
import json
d=json.loads(s)
print(d[0]["methods"][0]["smells"][0]["name"])
答案 2 :(得分:0)
这应该有效
data = [
{
"methods": [
{
"parametersTypes": [
"int",
"Menu"
],
"sourceFile": {
"file": {
"path": "/mnt/c/anyplace-master/android/actionbarsherlock/src/android/support/v4/app/Watson.java"
}
},
"metricsValues": {
"MethodLinesOfCode": 33.0,
"CyclomaticComplexity": 13.0,
"CouplingDispersion": 0.6666666666666666,
"MaxNesting": 5.0,
"ChangingClasses": 0.0,
"ParameterCount": 2.0,
"CouplingIntensity": 3.0,
"NumberOfAccessedVariables": 13.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0
},
"fullyQualifiedName": "android.support.v4.app.Watson.onCreatePanelMenu",
"smells": [
{
"name": "LongMethod",
"reason": "MLOC > 6.74646840148693",
"startingLine": 39,
"endingLine": 82
}
]
}
]
}
]
print(data[0]['methods'][0]['smells'][0]['name'])
您可能需要使用索引方法使其动态。您可以使用python的type
方法来检查数据的后续类型,例如type(data), type(data[0]), type(data[0]['methods'])
,这将有助于您更好地理解它。