如何将JSON文件解析为Python列表

时间:2019-04-19 21:38:41

标签: python json python-3.x

我正在尝试将JSON文件解析为Python,但无法打印我试图从JSON中提取的任何特定数据。

如何将这些JSON数据放在单独的数组中,以便可以在Python中使用它们?

这是JSON文件:

{
"Ask":
   {"0":[[9.13,30200],[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589]],
    "1":[[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589],[9.18,37521]] },

"Bid":
   {"0":[[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381],[9.08,98178]],
    "1":[[9.13,13500],[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381]]}
}

这是我的代码:

import json

with open("JSON Directory") as BOB:
    data=json.load(BOB)

for x in data["Bid"]:
    print(x["0"])

我收到此错误:

TypeError: string indices must be integers

4 个答案:

答案 0 :(得分:1)

for循环只有一个小问题。

james@VIII:~/Desktop$ ls
f.txt
james@VIII:~/Desktop$ cat f.txt 
{
"Ask":
   {"0":[[9.13,30200],[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589]],
    "1":[[9.14,106946],[9.15,53072],[9.16,58104],[9.17,45589],[9.18,37521]] },

"Bid":
   {"0":[[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381],[9.08,98178]],
    "1":[[9.13,13500],[9.12,198807],[9.11,1110],[9.1,42110],[9.09,84381]]}
}
james@VIII:~/Desktop$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> with open('f.txt') as f_in:
...     data = json.load(f_in)
... 
>>> data
{'Ask': {'0': [[9.13, 30200], [9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589]], '1': [[9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589], [9.18, 37521]]}, 'Bid': {'0': [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]], '1': [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]}}
>>> data['Ask']
{'0': [[9.13, 30200], [9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589]], '1': [[9.14, 106946], [9.15, 53072], [9.16, 58104], [9.17, 45589], [9.18, 37521]]}
>>> 
>>> data['Bid']
{'0': [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]], '1': [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]}
>>> for x in data['Bid']['0']:
...     print(x)
... 
[9.12, 198807]
[9.11, 1110]
[9.1, 42110]
[9.09, 84381]
[9.08, 98178]

您的for循环只需稍作更改。

在读取文件时不需要指定'r'的PS。

您还可以像这样获得单个值:

>>> for x in data['Bid']['0']:
...     print(str(x[0]) + ': ' + str(x[1]))
... 
9.12: 198807
9.11: 1110
9.1: 42110
9.09: 84381
9.08: 98178

答案 1 :(得分:0)

快速解答(TL; DR)

  • 转换之前

    for x in data["Bid"]:
      print(x["0"])
    
  • 进入之后

    for x in data["Bid"]:
      print(data["Bid"][x])
    

问题

  • 对加载的JSON数据的Python数据引用未产生预期的结果

解决方案

  • 正确地将JSON地址标识为字典键或字符串
  • 在原始示例中,print(x["0"])是不正确的引用,因为x是字符串(字典键)
  • 可以使用python pprint.pprint()对您要查找的内容进行故障排除来验证

示例

## import pprint 
## we use this to inspect the data to make sure we are
## working with what we expect

import pprint
pass

# [...] (loading code omitted)

for x in data["Bid"]:
  pprint.pprint(x)
# u'1'
# u'0'

修改示例

## pprint the values

for x in data.values():
  pprint.pprint(x)

# {u'0': [[9.13, 30200],
#         [9.14, 106946],
#         [9.15, 53072],
#         [9.16, 58104],
#         [9.17, 45589]],
#  u'1': [[9.14, 106946],
#         [9.15, 53072],
#         [9.16, 58104],
#         [9.17, 45589],
#         [9.18, 37521]]}
# {u'0': [[9.12, 198807],
#         [9.11, 1110],
#         [9.1, 42110],
#         [9.09, 84381],
#         [9.08, 98178]],
#  u'1': [[9.13, 13500],
#         [9.12, 198807],
#         [9.11, 1110],
#         [9.1, 42110],
#         [9.09, 84381]]}

已纠正示例

## we are looking at dictionary keys with `x`
## if we want to iterate the dictionary keys in data["Bid"]
## we need to correctly reference what we are looking for

for x in data["Bid"]:
  print(data["Bid"][x])

# [[9.13, 13500], [9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381]]
# [[9.12, 198807], [9.11, 1110], [9.1, 42110], [9.09, 84381], [9.08, 98178]]

答案 2 :(得分:0)

data["Bid"]是一个字典,并通过以下操作迭代一个字典:

for x in data["Bid"]:

您实际上是在遍历dict的键,这些键是字符串,并且无法使用x["0"]之类的非整数进行索引。

如果您打算遍历0字典的data["Bid"]键下的子列表,则应该执行以下操作:

for x in data["Bid"]['0']:
    print(x)

答案 3 :(得分:0)

您的for循环是dict键。

for x in data["Bid"]:
  print(type(x))
# <class 'str'>

尝试:

for x in data['Bid']['0']:
 print(x)

for x in data['Bid'].values():
 print(x)

对不起,我的英语:)