简体   繁体   中英

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. I then find out the problem happens when two items appear in one file

Here is the two JSON items:

{
    "黄南":{"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.

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.

Tried the most basic operations available in 3.4 and 2.7. It works without issue in 3.4. In 2.7, you need to use utf-8 or you'd get an error.

this will work with python 3.x but fail in 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

#!/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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM