json.loads正在加载一个json对象数组作为字符

时间:2018-03-23 20:30:58

标签: python json

我有代码

with open(filtered_aws_logs_path) as input:
    for i, line in enumerate(input):
        obj = json.loads("\"" + line[11:-7] + "\"")
        print("response: " + str(i))
        print(obj)
        for json_item in obj:
            print(json_item)
        break

该对象可以很好地加载到json中,当我用print(obj)打印它时,它看起来像:

[{"ip":"66.102.8.203","sg_user_id":7018833,"sg_event_id":"GxXXe3daQAq09ed5p-eprQ","sg_message_id":"b6eFR8T0Qqqz74USEIcbtQ.filter0005p3mdw1-17016-5AA04C0C-E.0","useragent":"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0 (via ggpht.com GoogleImageProxy)","event":"open","marketing_campaign_name":"Test Campaign","email":"emailer@gmail.com","asm_group_id":9611,"timestamp":1520527974,"marketing_campaign_id":2559395,"category":[]}]

但是当我尝试迭代数组时(在这种情况下只有1个对象),它只是逐行打印出字符。所以print(json_item)的开头是

[
{
"
i
p
"
:
"
6
6

这里发生了什么

1 个答案:

答案 0 :(得分:0)

obj仍然是一个JSON字符串。使用obj = json.loads(obj)再次解码。出于某种原因,您的日志具有双重编码的JSON。