2015-11-02 61 views
0

我陷入了这个简单代码的问题,无法调试问题。当我运行代码时,它给了我这个错误。ValueError:期望分隔符

ValueError: Expecting , delimiter: line 4 column 4 (char 20)

这里是我的代码:

import json 
    input=''' 
[ 
{ "id":"001" 
    "name":"nikhil" 
    "x":'2' 
}, 
{ "id":"002" 
    "name":"chuck" 
    "x":"2" 
} 
]''' 

info=json.loads(input) 
print "User count:",len(info) 

for item in info: 

    print "Name",item("name") 
    print "id",item("id") 
    print "Attribute",item("x") 
+1

JSON对象需要有每个按键之间的逗号:值对。 – khelwood

+0

是的你是对的,那就是问题所在。非常感谢你的帮助 –

回答

0

在我的代码中的错误是,我没有分离:用逗号“键值”对。我加了逗号,妥善缩进的代码,它的工作,

0

需要逗号

import json 
#here after "id" : "001" we have , 
    input=''' 
    [ 
    { "id":"001", 
     "name":"nikhil", 
     "x":'2' 
    }, 
    { "id":"002", 
     "name":"chuck", 
     "x":"2" 
    } 
    ]''' 

    info=json.loads(input) 
    print "User count:",len(info) 

    for item in info: 

     print "Name",item("name") 
     print "id",item("id") 
     print "Attribute",item("x")