简体   繁体   English

如何使用Python遍历JSON值?

[英]How can I iterate through JSON values using Python?

My JSON data looks like this: 我的JSON数据如下所示:

{
    "cameras": [
        {
            "item": "810-00022",
            "name": "front",
            "serial": "000287"
        },
        {
            "item": "810-00022",
            "name": "rear",
            "serial": "000166"
        },
        {
            "item": "810-00022",
            "name": "left",
            "serial": "000492"
        },
        {
            "item": "810-00022",
            "name": "right",
            "serial": "000282"
        },
        {
            "item": "810-00022",
            "name": "inside",
            "serial": "000143"
        }
    ],
    "item": "810-00023",
    "mac": "14:1f:ba:90:01:16",
    "name": 1623,
    "serial": "000408"
}
{
    "cameras": [
        {
            "item": "810-00032",
            "name": "inside",
            "serial": "000007"
        },
        {
            "item": "810-00022",
            "name": "right",
            "serial": "000941"
        },
        {
            "item": "810-00022",
            "name": "front",
            "serial": "000637"
        },
        {
            "item": "810-00022",
            "name": "left",
            "serial": "000430"
        }
    ],
    "item": "810-00023",
    "mac": "14:1f:ba:90:01:9e",
    "name": 1599,
    "serial": "000309"
}

How can I output the names (not camera names) for each of my entries? 如何输出每个条目的名称(不是摄像机名称)?

In theory, I want to be able to print the following 1623 and 1599 . 从理论上讲,我希望能够打印以下16231599

I have the following, but it is not working for some reason: 我有以下内容,但由于某些原因它无法正常工作:

json2 = open('C:\\Users\\' + comp_name + '\\Documents\\Python Firmware Script\\curl\\src\\systemidsout.json')
json2_obj = json.load(json2)

for i in json2_obj[]:
     print i['name']

I was hoping the above works, as it has for my other JSON file, but I'm guessing because the layout may be different, it's not working. 我希望上面的方法能像我其他JSON文件一样有效,但是我猜测是因为布局可能不同,所以无法正常工作。

How can I output the 'name' values within my JSON file? 如何在JSON文件中输出“名称”值?

Furthermore, as a bonus question, how can I output the individual names within my camera array also? 此外,作为一个额外的问题,如何在摄像机阵列中输出单个名称?

If you change for i in json2_obj[]: to i in json2_obj: , it will work. 如果您将for i in json2_obj[]:中的i in json2_obj:更改for i in json2_obj[]:中的i in json2_obj: ,它将起作用。 If you want to output the individual names within a camera array, use 如果要输出摄像机阵列中的各个名称,请使用

for j in i['cameras']:
    print(j['name'])

with in your for loop. 在您的for循环中。

Just to mention, your JSON data has actually 2 JSON. 只需提一下,您的JSON数据实际上就有2个JSON。 If you want to read them from one file, you might want to change it to [{...},{...}] format. 如果要从一个文件中读取它们,则可能需要将其更改为[{...},{...}]格式。 Otherwise, json.load() may raise an error. 否则, json.load()可能会引发错误。

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

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