python解析多层嵌套json,解析嵌套的JSON响应Python

I've been stuck on this all weekend, can anybody help me out please?

I'm trying to parse a nested JSON response. I cannot seem to return the values I need, I just get errors about "string indices must be integers" whenever I try to parse it...

What I am trying to achieve is: for each object in the JSON, extract the available_projects, and then the available_models from each. For example, the first one should be: model001, model_20171004-090552.

Sample JSON response:

{

"available_projects": {

"model001": {

"available_models": [

"model_20171004-090552"

],

"status": "ready"

},

"model002": {

"available_models": [

"model_20171013-143108"

],

"status": "ready"

},

"model002b": {

"available_models": [

"model_20171013-151458"

],

"status": "ready"

}

}

My Code:

myText = requests.get('http://localhost:5000/status')

jsonresponse = json.loads(myText.text)

for element in jsonresponse[u'available_projects']:

for AM in element[u'available_models']: ## this gives me the errors..

print AM

if I just do a "for element in jsonresponse[u'available_projects']: print element" statement, it correctly prints the available_projects list. How can I use that output to delve one level deeper into the JSON?

Any help, or a code snippet would be amazing - thank you!!

解决方案

You can use the

Example:

d = {

"available_projects": {

"model001": {

"available_models": [

"model_20171004-090552"

],

"status": "ready"

},

"model002": {

"available_models": [

"model_20171013-143108"

],

"status": "ready"

},

"model002b": {

"available_models": [

"model_20171013-151458"

],

"status": "ready"

}

}

}

for i in d["available_projects"].keys():

print i, "=" , d["available_projects"][i]['available_models'][0]

Output:

model001 = model_20171004-090552

model002b = model_20171013-151458

model002 = model_20171013-143108

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值