2012-10-04 29 views
6

io sto cercando di convertire i seguenti dict into JSON using json.dumps:JSON di dumping un dict dà TypeError: le chiavi devono essere una stringa

{ 
    'post_engaged': 36, 
    'post_impressions': 491, 
    'post_story': 23, 
    'comment_count': 6, 
    'created_time': '03:02 AM, Sep 30, 2012', 
    'message': 'Specialities of Shaktis and Pandavas. \n While having power, why there isn\\u2019t', 
    < built - in function id > : '471662059541196', 
    'status_type': 'status', 
    'likes_count': 22 
} { 
    'post_engaged': 24, 
    'text': '30 Sept 2012 Avyakt Murlli (Dual Voice)', 
    'post_story': 8, 
    'comment_count': 3, 
    'link': 'http:\\/\\/www.youtube.com\\/watch?v=VGmFj8g7JFA&feature=youtube_gdata_player', 
    'post_impressions': 307, 
    'created_time': '03:04 AM, Sep 30, 2012', 
    'message': 'Not available', 
    < built - in function id > : '529439300404155', 
    'status_type': 'video', 
    'likes_count': 7 
} { 
    'post_engaged': 37, 
    'post_impressions': 447, 
    'post_story': 22, 
    'comment_count': 4, 
    'created_time': '03:11 AM, Sep 30, 2012', 
    'message': '30-09-12 \\u092a\\u094d\\u0930\\u093e\\u0924:\\u092e\\u0941\\u0930\\u0932\\u0940 \\u0913\\u0', 
    < built - in function id > : '471643246209744', 
    'status_type': 'status', 
    'likes_count': 20 
} { 
    'post_engaged': 36, 
    'post_impressions': 423, 
    'post_story': 22, 
    'comment_count': 0, 
    'created_time': '03:04 AM, Sep 29, 2012', 
    'message': 'Essence: Sweet children, whenever you have time, earn the true income. Staying i', 
    < built - in function id > : '471274672913268', 
    'status_type': 'status', 
    'likes_count': 20 
} { 
    'post_engaged': 16, 
    'text': 'Essence Of Murli 29-09-2012', 
    'post_story': 5, 
    'comment_count': 2, 
    'link': 'http:\\/\\/www.youtube.com\\/watch?v=i6OgmbRsJpg&feature=youtube_gdata_player', 
    'post_impressions': 291, 
    'created_time': '03:04 AM, Sep 29, 2012', 
    'message': 'Not available', 
    < built - in function id > : '213046588825668', 
    'status_type': 'video', 
    'likes_count': 5 
} 

Ma mi porta a

TypeError : keys must be a string 

Credo che l'errore potrebbe essere popping perché il dict contiene, alcuni elementi come:

<built-in function id>: '213046588825668' 

Qualcuno può guidarmi per favore, con come dovrei rem ove questi elementi dal dict?

+3

correggere i dati. –

risposta

11

Si potrebbe provare a ripulirlo come questo:

for key in mydict.keys(): 
    if type(key) is not str: 
    try: 
     mydict[str(key)] = mydict[key] 
    except: 
     try: 
     mydict[repr(key)] = mydict[key] 
     except: 
     pass 
    del mydict[key] 

Questo tenterà di convertire qualsiasi tasto che non è una stringa in una stringa. Qualsiasi chiave che non può essere convertita in una stringa o rappresentata come una stringa verrà eliminata.

+0

Non è colpa tua, ma questo non farà ciò che il richiedente vuole * veramente *. –

+0

Ha apportato alcune modifiche, ma ha funzionato !! Grazie mille –

+4

L'unico modo per ottenere quell'oggetto funzione come chiave nel tuo dizionario è se hai digitato 'id' (che è una funzione built-in) invece di' "id" '(che è una stringa letterale) da qualche parte. Invece di cercare di aggirare il tuo bug esistente con più codice, forse dovresti cercare di capire dove si trova il codice che costruisce il dizionario? – Fredrik

-5

Forse questo aiuterà il prossimo ragazzo:

strjson = json.dumps(str(dic).replace("'",'"')) 
+0

questo è piuttosto brutto e dump anche una codifica json dei dati codificati json-ish, quindi la codifica json viene eseguita una volta su molti. – Herbert

Problemi correlati