Python JSON TypeError list indices must be integers or slices, not str

前端 未结 2 1281
北海茫月
北海茫月 2020-12-16 08:06

I am currently trying to parse some data from a post request response and I keep getting this error: \"TypeError: list indices must be integers or slices, not str\"

相关标签:
2条回答
  • 2020-12-16 08:59

    In my case:

    
        [
            {
                "_index": "abc",
                "_type": "abc",
                "_score": null,
                "_source": {
                  "layers": {
                    "frame_raw": [
                    "frame": {
                    ......
                    "raw": [
    
    

    Correct way to access "raw" values is

    
        data = json.load(json_file)
        data[0]['_source']['layers']['raw'][0]
        data[0]['_source']['layers']['raw'][1]
    
    ...
    

    In case of multiple data:

    
        [
            {
                "_index": "abc",
                "_type": "abc",
                "_score": null,
                "_source": {
                  "layers": {
                    "frame_raw": [
                    "frame": {
                    ......
                    "raw": [
    
            {
                "_index": "abc",
                "_type": "abc",
                "_score": null,
                "_source": {
                  "layers": {
                    "frame_raw": [
                    "frame": {
                    ......
                    "raw": [
    
    

    Get it by:

    
        data = json.load(json_file)
                for d in data:
                    print(d['_source']['layers']['raw'][0])
    
    
    0 讨论(0)
  • 2020-12-16 09:06

    data['result']['results'] is an array so you can't do ['name'] you need an int, you could add [0] after['results'] and it should work. Then you can reference keys within the object in results.

    0 讨论(0)
提交回复
热议问题