unhashable类型:带有测试JSON的dict

时间:2013-11-14 01:45:36

标签: python json

当我尝试在python解释器中测试一些JSON时,我得到了错误。我不知道为什么。

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
TypeError: unhashable type: 'dict'

JSON :(不起作用)

b = {
        'data':{
                'child':{
                        {'kid1':'one'},
                        {'kid2':'two'},
                        {'kid3':'three'}
                },
                'child':{
                        {'kid4':'four'},
                        {'kid5':'five'},
                        {'kid6':'six'}
                }
        }
    }

JSON :(有效)

a = {
     "slate" : {
         "id" : {
             "type" : "integer"
         },
         "name" : {
             "type" : "string"
         },
         "code" : {
             "type" : "integer",
            "fk" : "banned.id"
         }
     },
     "banned" : {
         "id" : {
             "type" : "integer"
         },
         "domain" : {
             "type" : "string"
         }
     }
 }

2 个答案:

答案 0 :(得分:7)

你的第一个例子不起作用的原因是每个'child'键都有一个字典声明为它的值而不是列表,因为它看起来像你想要的那样。将{替换为[即可。

'child': {
    {'kid1':'one'},
    {'kid2':'two'},
    {'kid3':'three'},
},

应该是:

'child': [
    {'kid1':'one'},
    {'kid2':'two'},
    {'kid3':'three'},
],

换句话说,你说“孩子”是一本没有字典的词典。

答案 1 :(得分:0)

当我的JSON格式略有错误时,这个问题对我来说就出现了:

json = {
    {
        "key": "value",
        "key_two": "value_two"
    }
}

应该是:

json = {
    "key": "value",
    "key_two": "value_two"
}