JSON 数据解析时遇到ValueError: No JSON object could be decoded

来源:互联网 发布:网络教育 学籍 编辑:程序博客网 时间:2024/06/05 08:53
Traceback (most recent call last):
  File "dataDeal.py", line 28, in <module>
    createJson("/home/wcs/train2014.json","/home/wcs/train")
  File "dataDeal.py", line 8, in createJson
    data=json.load(f)
  File "/home/wcs/anaconda2/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
  File "/home/wcs/anaconda2/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/home/wcs/anaconda2/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/wcs/anaconda2/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")

ValueError: No JSON object could be decoded


我遇到这个错误是因为JSON手写出错大概是这样

{  
   "info":{  
      "description":"the train dog images",
      "url":,
      "version":"1.0",
      "year":2017
   },
   "images":[]
}

在我的url 那里不能空着只留下逗号在,所以估计遇到这个错误的一个原因应该是JSON 文件有误,检查你写的是否有误请把你的内容复制到这里https://jsonformatter.curiousconcept.com/    在线检查一下就好了

下面再附上我写JSON文件的程序作为参考,这个是制作图片信息的程序

import json
import os
from PIL import Image


def createJson(path,dogPath):
with open(path,'r') as f:
data=json.load(f)
imagesInfo=[]
typeFiles=os.listdir(dogPath)
for dog in typeFiles:
dogImages=os.listdir(os.path.join(dogPath,dog))
for dogImg in dogImages:
imgPath=os.path.join(dogPath,dog,dogImg)
img=Image.open(imgPath)
width=img.size[0]
height=img.size[1]
idNum=dogImg.split('.')[0]
dogDict={'license':dog,'file_name':dogImg,'height':height,'width':width,'id':idNum}
imagesInfo.append(dogDict)
data['images']=imagesInfo
with open(path,'w') as file:
json.dump(data,file)

createJson("/home/wcs/train2014.json","/home/wcs/train")

第一个参数不用说就是你要写入的JSON文件,第二个参数是传入的图片文件路径 train 下还有很多文件夹,里面是图片

结果会是这样

{"info": {"version": "1.0", "description": "the val dog images", "year": 2017}, "images": [{"id": "1601053770,2434600106", "file_name": "1601053770,2434600106.jpg", "width": 636, "license": "81", "height": 433}, {"id": "566704649,1846734015", "file_name": "566704649,1846734015.jpg", "width": 350, "license": "81", "height": 450}, {"id": "3134450397,2844321447", "file_name": "3134450397,2844321447.jpg", "width": 600, "license": "81", "height": 387}, {"id": "1181240440,1441438354", "file_name": "1181240440,1441438354.jpg", "width": 780, "license": "81", "height": 517}, {"id": "1536683132,3786087832", "file_name": "1536683132,3786087832.jpg", "width": 575, "license": "81", "height": 512}, {"id": "608206698,1961451584", "file_name": "608206698,1961451584.jpg", "width": 529, "license": "81", "height": 800}, {"id": "2909808636,1613402380", "file_name": "2909808636,1613402380.jpg", "width": 300, "license": "81", "height": 300}, {"id": "2151502926,1713523353", "file_name": "2151502926,1713523353.jpg", "width": 600, "license": "81", "height": 339}, {"id": "326703502,1581517315", "file_name": "326703502,1581517315.jpg", "width": 640, "lic



阅读全文
0 0
原创粉丝点击