Python Json加载表示这是无效的Json
我试图获取Python Json加载表示这是无效的Json,python,arrays,json,python-2.7,Python,Arrays,Json,Python 2.7,我试图获取json.loads()来读取包含上述文本块的文件,并将其解析为包含上述信息的字典数组。然而,当我试着运行它时,它只是说 [ { "category_id": "", "ad_type": "offer", "price": "1000", "sale_by": "owner", "title": "Test title", "description": "", "l
json.loads()
来读取包含上述文本块的文件,并将其解析为包含上述信息的字典数组。然而,当我试着运行它时,它只是说
[
{
"category_id": "",
"ad_type": "offer",
"price": "1000",
"sale_by": "owner",
"title": "Test title",
"description": "",
"location": "washington",
"postal_code": "1001",
"address": "",
"images": [
"User/Desktop/image1.jpg",
"User/Desktop/image2.jpg"
]
}
]
但是复制并粘贴到中表示它是一个有效的JSON,所以我不确定出了什么问题
这是我的代码:
ValueError: No JSON object could be decoded
确保它是一个字符串
with open(source_data) as data_file:
data = json.loads(data_file.read())
输出:
a = '''[
{
"category_id": "",
"ad_type": "offer",
"price": "1000",
"sale_by": "owner",
"title": "Test title",
"description": "",
"location": "washington",
"postal_code": "1001",
"address": "",
"images": [
"User/Desktop/image1.jpg",
"User/Desktop/image2.jpg"
]
}
]'''
数据很好,所以问题一定出在你如何读取数据上。请发布您的代码。您在此处粘贴的内容可以与
json一起使用。加载
表示您正在传递其他内容。您是如何读取该文件的?能否显示您的python代码json.loads
用于字符串,而不是文件对象我已经在主帖子中更新了我的代码打印(data\u file.read())
给了你什么…?
json.loads(a)
[{u'description': u'', u'title': u'Test title', u'price': u'1000', u'ad_type': u'offer', u'sale_by': u'owner', u'location': u'washington', u'address': u'', u'images': [u'User/Desktop/image1.jpg', u'User/Desktop/image2.jpg'], u'postal_code': u'1001', u'category_id': u''}]