2016-11-08 33 views
-3
[ 
    { 
     "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.loads()讀取包含上述文本塊的文件,並將其解析爲包含上述信息的字典的數組無效JSON。然而,當我嘗試運行它,它只是說Json的負載是說這是

ValueError: No JSON object could be decoded 

但複製並粘貼到http://jsonlint.com/此說,這是一個有效的JSON,所以我不知道什麼是錯的..

這是我的代碼:

with open(source_data) as data_file: 
    data = json.loads(data_file.read()) 
+1

的數據是好的,所以這個問題必須在你是如何閱讀它。請發佈您的代碼。 –

+0

你在這裏粘貼的東西適用於'json.loads',這意味着你傳遞了其他東西。你如何閱讀文件? –

+0

你可以請求顯示你的python代碼嗎? 'json.loads'是一個字符串,而不是一個文件對象 –

回答

2

確保它是一個字符串。

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.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''}]