简体   繁体   English

在 python 中如何从 json 获取元素数组中的所有值

[英]In python how to get all the values in an array of elements from json

Below is the json with array of elements.下面是带有元素数组的 json。 How to get all the name values in a array?如何获取数组中的所有名称值? Is there a simplar way of doing it without for loop.有没有没有 for 循环的简单方法。

import json
from unicodedata import name

# Define json variable
jsondata = """[
 {
  "name":"Pen",
  "unit_price":5
 },
 {
  "name":"Eraser",
  "unit_price":3
 },
 {
  "name":"Pencil",
  "unit_price":10
 },
 {
  "name":"White paper",
  "unit_price":15
 }
]"""

# load the json data
items = json.loads(jsondata)
namelist = []
for keyval in items:
    namelist.append((keyval['name']))

print(namelist)
names = [it['name'] for it in items]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM