简体   繁体   English

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

[英]python output string from server into dictionary

how to convert a string such as below 如何转换如下的字符串

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}}}}'

into a dictionary ? 进字典?

Did you copy that from a python interpreter session, or are the >>> s really part of the string? 你是从python解释器会话中复制它,还是>>> s真的是字符串的一部分?

If so, then this looks like json, so why not use json.loads() : 如果是这样,那么这看起来像json,那么为什么不使用json.loads()

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

You should remove the >>> and then use json.loads() 你应该删除>>>然后使用json.loads()

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

Otherwise you you also use eval() , but that wouldn't be a nice and safe solution. 否则你也使用eval() ,但这不是一个好的和安全的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM