简体   繁体   中英

How to get the position of a string in a JSON file?

I'm currently working on a project, written in python, for my class, which involves getting JSON data from a server.

My answer from the server looks something like this but is about 700000 characters long:

{
"jsonrpc": "2.0",
"id": "123",
"result": [{
        "id": 666666,
        "date": 20170627,
        "startTime": 1120,
        "endTime": 1140,
        "lstype": "bs",
        "kl": [],
        "su": [],
        "ro": [{
            "id": 111,
            "name": "ROOM_2",
            "longname": "NAME_BBB"
        }],
        "lsnumber": 111,
        "statflags": "@"
    },

    {
        "id": 666667,
        "date": 20170626,
        "startTime": 950,
        "endTime": 1035,
        "kl": [{
            "id": 222,
            "name": "CLASS_1",
            "longname": "TEACHER_NAME_1, TEACHER_NAME_2"
        }],
        "su": [{
            "id": 33,
            "name": "XXX",
            "longname": "SUBJECT_1"
        }],
        "ro": [{
            "id": 44,
            "name": "ROOM_1",
            "longname": "NAME_AAA"
        }],
        "lsnumber": 55555,
        "statflags": "@"
    },

    {
        "id": 666668,
        "date": 20170627,
        "startTime": 1225,
        "endTime": 1310,
        "kl": [{
            "id": 666,
            "name": "CLASS_2",
            "longname": "TEACHER_NAME_3"
        }],
        "su": [{
            "id": 777,
            "name": "XXX",
            "longname": "SUBJECT_2"
        }],
        "ro": [{
            "id": 88,
            "name": "ROOM_3",
            "longname": "NAME_BBB, NAME_CCC"
        }],
        "lsnumber": 99999,
        "statflags": "@"
    },

    {
        "id": 666669,
        "date": 20170627,
        "startTime": 1500,
        "endTime": 1545,
        "kl": [{
                "id": 122,
                "name": "CLASS_1",
                "longname": "TEACHER_NAME_1, TEACHER_NAME_2"
            },
            {
                "id": 133,
                "name": "CLASS_3",
                "longname": "TEACHER_NAME_5, TEACHER_NAME_6"
            },
            {
                "id": 144,
                "name": "CLASS_4",
                "longname": "TEACHER_NAME_7 / TEACHER_NAME_8"
            }
        ],
        "su": [{
            "id": 55,
            "name": "XXX",
            "longname": "SUBJECT_3"
        }],
        "ro": [{
            "id": 66,
            "name": "ROOM_4",
            "longname": "NAME_DDD"
        }],
        "lsnumber": 7777,
        "statflags": "@"
    }

]

}

I need to get all positions of CLASS_1 in my JSON file so I can get the relating information (startTime, endTime, SUBJECT_X, ROOM_X and NAME_XXX) to display them in my application.

The output should look like this:

match 1:
950 till 1035
SUBJECT_1
ROOM_1
NAME_AAA

match 2:
1500 till 1545
SUBJECT_3
ROOM_4
NAME_DDD

甚至不要尝试将json解析为字符串-使用json模块将其转换为Python json.load()根据您的来源是字符串还是打开的文件,使用json.loads()json.load() ),然后遍历结果对象(在这种情况下是dict )以获取您的值。

You could do something like this,

result = []
for item in data['result']:
    try:
        if item['kl'][0]['name'] == 'CLASS_1':
             result.append(['{} till {}'.format(item['startTime'], item['endTime']),
                      item['su'][0]['longname'], item['ro'][0]['name'],
                      item['ro'][0]['longname']])
    except:
        pass


In [11]: result
Out[11]: 
[['950 till 1035', 'SUBJECT_1', 'ROOM_1', 'NAME_AAA'],
 ['1500 till 1545', 'SUBJECT_3', 'ROOM_4', 'NAME_DDD']]

data would be the dictionary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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