如何在python中从JSON中获取一个键和值?
当我在django中点击API URL时,我得到了一个数据作为响应。我想从该数据中获取content_id,以检查它是否已经存在。如何迭代JSON以找到特定的键及其值如何在python中从JSON中获取一个键和值?,python,django,Python,Django,当我在django中点击API URL时,我得到了一个数据作为响应。我想从该数据中获取content_id,以检查它是否已经存在。如何迭代JSON以找到特定的键及其值 r.json() = { "code": 200, "status": "OK", "data": [ { "cart_id": "36", "content": [
r.json() =
{
"code": 200,
"status": "OK",
"data": [
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "17408"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
}
],
"msg": "Contents Found!"
}
我尝试了
r.json()['data'][0]['content'][0]
,但它只在中的一个数据中工作,如果有多个数据,那么当我尝试使用for循环迭代内容id时,它就不起作用了使用python,将json转换为字典并以这种方式进行迭代非常容易
使用python,将json转换为字典并以这种方式进行迭代非常容易
也许像这样的事情会有所帮助。在您发布的代码中,第一行与我之前看到的有点不同。也许json.loads将帮助您获取内容中n个对象的“content_id”
import json
data = json.loads('''{
"code": 200,
"status": "OK",
"data": [
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "17408"
},
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "999834"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
},
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "34523"
},
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "6423412"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
}
],
"msg": "Contents Found!"
}''')
for current_data in data["data"]:
for current_content in current_data["content"]:
# here I am printing, I imagine you will do something different
# with the content_id.
print(current_content["content_id"])
也许像这样的事情会有帮助。在您发布的代码中,第一行与我之前看到的有点不同。也许json.loads将帮助您获取内容中n个对象的“content_id”
import json
data = json.loads('''{
"code": 200,
"status": "OK",
"data": [
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "17408"
},
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "999834"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
},
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "34523"
},
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "6423412"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
}
],
"msg": "Contents Found!"
}''')
for current_data in data["data"]:
for current_content in current_data["content"]:
# here I am printing, I imagine you will do something different
# with the content_id.
print(current_content["content_id"])
一般访问
with open('vash.json') as f:
data = json.load(f)
data['data'][0]['content'][0]['content_id'])
反复
for i in data['data']:
for j in i['content']:
print(j['content_id'])
奖金
一般访问
with open('vash.json') as f:
data = json.load(f)
data['data'][0]['content'][0]['content_id'])
反复
for i in data['data']:
for j in i['content']:
print(j['content_id'])
奖金
到目前为止你都试了些什么?@KlausD。我更新了问题。你要给我们看不起作用的代码吗?@KlausD。这是一个顶部有
r.json()
的有效json文件吗?我很熟悉json的导航,但从未见过它有这个顶部line@vash_the_stampede上面的作业是垃圾。我猜他想表达的是,如果给他打电话,这就是结果。到目前为止你试过什么?@KlausD。我更新了问题。你要给我们看不起作用的代码吗?@KlausD。这是一个顶部有r.json()
的有效json文件吗?我很熟悉json的导航,但从未见过它有这个顶部line@vash_the_stampede上面的作业是垃圾。我猜他想表达的是,如果给定的调用,这就是结果。