2017-09-14 22 views
0

我無法找出爲什麼下面的代碼行不起作用:如何解決錯誤「ValueError:期望,定界符」?

text = '''{"seasons":"[{"season-type":"type1","start-day":10,"start-month":1,"end-day":31,"end-month":5},{"season-type":"type2","start-day":1,"start-month":9,"end-day":9,"end-month":1},{"season-type":"type3","start-day":1,"start-month":6,"end-day":30,"end-month":9}]"}''' 
obj = json.loads(text) 

obj 

我收到以下錯誤ValueError: Expecting , delimiter: line 1 column 16 (char 15)

+2

你似乎已經引用列表,它是價值的鍵'「季節」'是指。 – quamrana

+0

它必須是'text ='''{「seasons」:[{「season-type」...]}'''' –

+0

@quamrana:好的,我明白了。我怎樣才能將它轉換成有效的JSON字符串? – Dinosaurius

回答

1

這似乎工作:

import json 

text = '''{"seasons":[{"season-type":"type1","start-day":10,"start-month":1,"end-day":31,"end-month":5},{"season-type":"type2","start-day":1,"start-month":9,"end-day":9,"end-month":1},{"season-type":"type3","start-day":1,"start-month":6,"end-day":30,"end-month":9}]}''' 

obj = json.loads(text) 

你的問題有各地的列表雙引號:

text = '''{"seasons":"[{"season-type": ... ,"end-month":9}]"}''' 
        ^         ^
0

你必須在字符串中額外的引號。試試這個

{ 
"seasons": [ 
    {"season-type":"type1","start-day":10,"start-month":1,"end-day":31,"end-month":5}, 
    {"season-type":"type2","start-day":1,"start-month":9,"end-day":9,"end-month":1}, 
    {"season-type":"type3","start-day":1,"start-month":6,"end-day":30,"end-month":9}] 
} 
相關問題