7

When I try to test some JSON in the python interpreter I get the error. I'm not sure why.

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

JSON: (doesn't work)

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

JSON: (works)

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

2 回答 2

8

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

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

应该:

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

换句话说,您说“孩子”是一本字典,但没有给出字典。

于 2013-11-14T01:52:05.887 回答
1

当我的 JSON 格式稍有错误时,我遇到了这个问题:

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

应该:

json = {
    "key": "value",
    "key_two": "value_two"
}
于 2020-10-09T22:18:12.853 回答