使用python在GUI上导入json文件。得到JSONDecodeError

时间:2018-05-21 11:55:06

标签: python json anaconda

Link to the .json file - >这是.json文件的链接。

我正在制作一个基于Python的项目,我在这里尝试做的是使用.json文件。但是,当我尝试导入它以使用GUI时,我收到了以下错误。

提前谢谢你。  回溯(最近一次调用最后一次):

  File "<ipython-input-1-31816de2c4db>", line 1, in <module>
    runfile('C:/Users/Akshita/pyex/Webmap/Gui.py', wdir='C:/Users/Akshita/pyex/Webmap')

 File "C:\Users\Akshita\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
   execfile(filename, namespace)

 File "C:\Users\Akshita\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

 File "C:/Users/Akshita/pyex/Webmap/Gui.py", line 12, in <module>
   data=json.load(open(r"C:\Users\Akshita\pyex\Webmap\world.json"))

 File "C:\Users\Akshita\Anaconda3\lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)

  File "C:\Users\Akshita\Anaconda3\lib\json\__init__.py", line 354, in loads
   return _default_decoder.decode(s)

 File "C:\Users\Akshita\Anaconda3\lib\json\decoder.py", line 339, in decode
  obj, end = self.raw_decode(s, idx=_w(s, 0).end())

 File "C:\Users\Akshita\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
  raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError:期望值

# -*- coding: utf-8 -*-
"""
Created on Mon May 21 10:15:01 2018

@author: Akshita
"""

from tkinter import *
import json
import difflib
from difflib import get_close_matches
data=json.load(open(r"C:\Users\Akshita\pyex\Webmap\world.json"))
window=Tk()
window.title("Webmap")
l1=Label(window,text="Name_of_place")
l1.grid(row=0,column=0)
val=StringVar()
e=Entry(window,textvariable=val)
e.grid(row=0,column=1)
be=Button(window,text="Enter",command=entry)
be.grid(row=0,column=2)
def entry():

    w= e_val.get()

    w=w.title()

    if w in data:
     t.insert("Data matched")
    elif len(get_close_matches(w,data.keys(),cutoff=0.8))>0:
        t.insert("Did you mean %s instead?" %get_close_matches(w,data.keys())[0])  

t=Text(window,height=3,width=20)
t.grid(row=1,column=1)
by=Button(window,text="Yes",command=yeah)
by.grid(row=2,column=1)
def yeah():
    t.insert("Processing")

bn=Button(window,text="No",command=nah)
bn.grid(row=2,column=2)

def nah():
    t.insert("No such Data")

window.mainloop()

1 个答案:

答案 0 :(得分:0)

看起来您的JSON文件无效 - 最后的括号序列不正确。

结尾应如下所示:

... <previous data> ... ,[2.96361,36.802216]]]}}]}

您可以使用在线或IDE JSON检查程序验证您的文件。

更新

获得完整的json文件。

事实证明它有UTF-8 BOM头。以这种方式尝试load()

import json
import codecs

data = json.load(codecs.open('tst.json', 'r', 'utf-8-sig'))
print(data)

相关SO帖子:https://stackoverflow.com/a/13156715/2315573