如何使用python中的字符串列表读取json文件

时间:2018-07-06 11:37:21

标签: python json

这是我从python中的defaultdict创建的json数据的示例。

[{
    "company": [
        "ABCD"
    ],
    "fullname": [
        "Bruce Lamont",
        "Ariel Zilist",
        "Bruce Lamont",
        "Bobby Ramirez"
    ],
    "position": [
        " The Hesh",
        " Server",
        " HESH",
        " Production Assistant"
    ],
    "profile_url": [
        "http://www.url1.com",
        "http://www.url2.com",
        "http://www.url3.com",
        "http://www.url4.com",
    ]
}]

我意识到我在创建此类列表时出错。 json.loads()出现此错误

错误

  

期望值:第1行第1列(字符0)。

我想要这样的东西。

[{
    "company": [
        "name": "THALIA HALL",
        "employee": {
            fullname: "emp_name",
            "position": "position",
            profile: "url"
        },
        {
            fullname: "emp_name",
            "position": "position",
            profile: "url"
        }
    ]
}]

如何解决此问题?我需要在python上执行此操作。

2 个答案:

答案 0 :(得分:1)

您最后在profile_url数组中添加了一个逗号。正确的json应该是

[{
    "company": [
        "ABCD"
    ],
    "fullname": [
        "Bruce Lamont",
        "Ariel Zilist",
        "Bruce Lamont",
        "Bobby Ramirez"
    ],
    "position": [
        " The Hesh",
        " Server",
        " HESH",
        " Production Assistant"
    ],
    "profile_url": [
        "http://www.url1.com",
        "http://www.url2.com",
        "http://www.url3.com",
        "http://www.url4.com"
    ]
}]

下次使用https://jsonformatter.curiousconcept.com/检查JSON格式错误。

答案 1 :(得分:0)

import json

j  = '[{"company":["ABCD"],"fullname":["Bruce Lamont","Ariel Zilist","Bruce Lamont","Bobby Ramirez"],"position":[" The Hesh"," Server"," HESH"," Production Assistant"],"profile_url":["http://www.url1.com","http://www.url2.com","http://www.url3.com","http://www.url4.com"]}]'


json_obj = json.loads(j)

您将JSON作为对象,现在您可以使用用户制作csv