python将字符串从服务器输出到字典中

2024-05-18 06:11:26 发布

您现在位置:Python中文网/ 问答频道 /正文

如何转换如下字符串

str1='{"data":{"infostore":{"view":"infostore/split"},"mail":>>>{"auto_save_drafts":3,"select_first_message":true,"showname":false,"htmlmessage":true,"signatures":[],"view":"mail/hsplit/unthreaded","autocomplete":true,"formatmessage":"ALTERNATIVE","editor_featureset":2,"fullmailheader":true,"notifyacknoledge":true},"portal":{"externalcontents":[],"internalcontents":[{"module":"mail","visible":true,"external":false,"adj":{"y":0,"x":0},"params":{"limit":5},"header":"E-Mail"},{"module":"calendar","visible":true,"external":false,"adj":{"y":0,"x":1},"params":{"limit":5},"header":"Calendar"},{"module":"contacts","visible":false,"external":false,"adj":{"y":1,"x":2},"params":{"limit":5},"header":"Contacts"},{"module":"infostore","visible":true,"external":false,"adj":{"y":1,"x":0},"params":{"limit":5},"header":"InfoStore"},{"module":"tasks","visible":true,"external":false,"adj":{"y":1,"x":1},"params":{"limit":5},"header":"Tasks"}]},"global":{"minicalendar":{"expanded":false},"confirmpopup":true,"save":1,"autorefresh":10,"landing_page":{"module":"portal"}},"menu":{"menuiteration":2},"wizard":{"firstrun":false,"launchOnStart":false},"categories":{"local":[]},"effects":{"global":false,"hover":{"infostore":true,"mail":false,"speed":3,"portal":true,"tasks":true,"contacts":true,"calendar":true},"fading":false},"tasks":{"interval":30,"gridsort":"asc","view":"tasks/split"},"contacts":{"gridsort":"asc","cardsToViewPerColumn":"auto","view":"contacts/cards"},"calendar":{"workweek":{"countdays":5,"numberofappointments":2,"startday":1},"endtime":18,"autoadd_participant_public":true,"allfolders":true,"starttime":6,"default_reminder":15,"interval":30,"views":{"shared":true,"list":"workweek","team":"workweek","view":"calendar","calendar":"workweek"},"teamview":{"workingTimeOnly":true},"day":{"numberofappointments":4},"view":"calendar/calendar/workweek","custom":{"countdays":3,"numberofappointments":3}}}}'

查字典?你知道吗


Tags: viewfalsetruemailparamscalendarexternalheader
2条回答

您应该删除>>>,然后使用json.loads()

import json
str1 = str1.replace('>>>', '')
dict1 = json.loads(str1)

否则,您也可以使用eval(),但这不是一个好的、安全的解决方案。你知道吗

您是从python解释器会话复制的,还是>>>真的是字符串的一部分?你知道吗

如果是这样,那么这看起来像是json,那么为什么不使用^{}

import json
str1 = ...
obj = json.loads(str1)

相关问题 更多 >