[英]Unexpected token error when combining two valid json object
I am doing a web application which needs to load a JSON file from the server, when I try to parse that file I encountered the Unexpected Token
error. 我正在做一个Web应用程序,当我尝试解析该文件时,我需要从服务器加载JSON文件,我遇到了
Unexpected Token
错误。 I then find out the problem happens when two items appear in one file 然后我发现当一个文件中出现两项时会发生问题
Here is the two JSON items: 这是两个JSON项目:
{
"黄南":{"id":10973,"name":"黄南","prov":"青海","latt":35.519549,"logi":102.015248},
"海北":{"id":10970,"name":"海北","prov":"青海","latt":36.954413,"logi":100.900998}
}
I have tried JSON.parse
in chrome console, json.loads
in python and JSONlint.com , they all raise errors. 我曾尝试
JSON.parse
铬控制台, json.loads
Python和JSONlint.com ,他们都引发错误。
The interesting part is that when I tried to load them individually, there was no error, but as long as they are loaded together, the error is thrown out 有趣的部分是,当我尝试分别加载它们时,没有错误,但是只要将它们一起加载,就会抛出错误
So can anyone tell me what is happening and how to avoid this? 那么谁能告诉我发生了什么以及如何避免这种情况? Thank you guys and sorry if there is any gramma issues in my description.
谢谢大家,如果我的描述中有任何语法问题,我们深表歉意。
It's a guess without more info, but I'm guessing python 2.x without setting the encoding, because other than that there is no other reason. 这是没有更多信息的猜测,但是我猜测python 2.x没有设置编码,因为除此之外,没有其他原因。
Tried the most basic operations available in 3.4 and 2.7. 尝试了3.4和2.7中可用的最基本的操作。 It works without issue in 3.4.
它在3.4中没有问题。 In 2.7, you need to use utf-8 or you'd get an error.
在2.7版中,您需要使用utf-8,否则会出现错误。
this will work with python 3.x but fail in 2.x 这将适用于python 3.x,但在2.x中失败
#!/usr/bin/env python3.4
import json
j = """{
"黄南":{"id":10973,"name":"黄南","prov":"青海","latt":35.519549,"logi":102.015248},
"海北":{"id":10970,"name":"海北","prov":"青海","latt":36.954413,"logi":100.900998}
}"""
o = json.loads(j)
print(json.dumps(o,indent=1))
This will work in 2.7, probably 2.6 too 这将在2.7或2.6中运行
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import json
j = """{
"黄南":{"id":10973,"name":"黄南","prov":"青海","latt":35.519549,"logi":102.015248},
"海北":{"id":10970,"name":"海北","prov":"青海","latt":36.954413,"logi":100.900998}
}"""
o = json.loads(j)
print(json.dumps(o,indent=1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.