【问题标题】:How can I parse json array in python? [duplicate]如何在 python 中解析 json 数组? [复制]
【发布时间】:2016-02-09 00:49:27
【问题描述】:
[
    {
        "key": "abc123",
        "columns": [
            [
                "2015-08-05 12\\:38\\:02+0000:",
                "",
                1439565881058000
            ],
            [
                "2015-08-05 12\\:38\\:02+0000:type",
                "1",
                1439565881058000
            ],
            [
                "2015-08-05 12\\:38\\:02+0000:duration",
                "21",
                1439565881058000
            ],
            [
                "2015-08-05 12\\:38\\:02+0000:first_name",
                "abc",
                1439565881058000
            ],
            [
                "2015-08-05 12\\:38\\:02+0000:last_name",
                "xyz",
                1439565881058000
            ],
            [
                "2015-08-05 12\\:38\\:02+0000:number",
                "012456789",
                1439565881058000
            ]
        ]
    }
]

【问题讨论】:

  • 到目前为止你尝试过什么?如果您的问题是关于特定问题而不是一般问题,那么您更有可能获得有用的答案。

标签: python shell apache-pig


【解决方案1】:

使用json库

data = json.loads(your_jsondata) 

你可以通过调用它来访问你的 json 数据

data[0]['key']

获取密钥

【讨论】:

  • 我已经完成了上述操作,但仍然出现以下错误 >>> import json >>> from pprint import pprint >>> >>> with open('/Users/xxx/Documents/ sample.json') as data_file: ... data = json.load(data_file) ... >>> data['key'] Traceback(最近一次调用最后):文件“”,第 1 行,在 TypeError: list indices must be integers, not str
  • @SarthakPrakash 你拥有的是一个数组,所以你应该把它叫做 data[0]['key']
  • 谢谢!我必须做什么才能提取列值??
  • @SarthakPrakash 只使用列而不是键,您将获得列数组 data[0]['columns']
猜你喜欢
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多