正在将所有dict值转换为str



我有一个类方法将字典值转换为str,为将其作为post数据添加到formdata做准备,但是,有些值没有从int转换为str,例如:

jsonData={
"LanguageId":1,
"ClientTypeId":2,
"BrandId":3,
"JurisdictionId":1,
"ClientIntegratorId":1,
"ExternalIds":[10372246,10372249,10372236,10372239,10372234,10372235,10372231,10372233,10377244,10377245,10377254,10377247,10377251,10377248,10377249,10377246
],
"MarketCName":"win-draw-win",
"ScoreboardRequest":{
"ScoreboardType":3,
"IncidentRequest":{}
},
"BrowserId":2,
"OsId":4,
"ApplicationVersion":"",
"BrowserVersion":"15.4",
"OsVersion":"10.15.7",
"SessionId":'null',
"TerritoryId":227,
"CorrelationId":"71a5a843-a9cd-4152-9ef5-cede7ee59e33",
"VisitId":"b0d95dbe-fc0c-482a-a45c-1e300c8682b9",
"ViewName":"sports",
"JourneyId":"77057d08-5dff-4da4-afd7-087b97024194"
}

这是class:

class StringDict(dict):
def __init__(self):
dict.__init__(self)

def __getitem__(self, y):
value_to_string = str(dict.__getitem__(self, y))
return value_to_string

def get(self, y):
if isinstance(dict.get(self, y), list):
value_to_string = [str(x) for x in dict.get(self, y)]

elif isinstance(dict.get(self, y), dict):
value_to_string = {}
for keys, values in dict.get(self, y).items():
if isinstance(values, dict):
values_to_string[keys] = values
values_to_string[keys] = str(values)

elif isinstance(dict.get(self, y), int) or isinstance(dict.get(self, y), float):
value_to_string = str(dict.get(self, y))
else:
value_to_string = str(dict.get(self, y))
return value_to_string
exampleDict = StringDict()

测试:

for key, values in jsonData.items():
exampleDict[key] = values
[type(x) for x in {**exampleDict}.values()]
[int,
int,
int,
int,
int,
list,
str,
dict,
int,
int,
str,
str,
str,
str,
int,
str,
str,
str,
str]

我应该只有一个list, dict, str。其中list内部的所有值都是strdict也是如此。

预期输出:

[str,
str,
str,
str,
str,
list,
str,
dict,
str,
str,
str,
str,
str,
str,
str,
str,
str,
str,
str]

listdict中的所有值也应该是str

您需要为values()添加一个__iter__方法来调用您的__getitem__:

def __iter__(self):
return dict.__iter__()

然后打印:

[<class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>, <class 'str'>]

您可以尝试简单的递归来转换dict:

def convert(o):
if isinstance(o, dict):
return {
k: convert(v) if isinstance(v, (list, dict)) else str(v)
for k, v in o.items()
}
elif isinstance(o, list):
return [
convert(v) if isinstance(v, (list, dict)) else str(v) for v in o
]

print(convert(jsonData))

打印:

{
"LanguageId": "1",
"ClientTypeId": "2",
"BrandId": "3",
"JurisdictionId": "1",
"ClientIntegratorId": "1",
"ExternalIds": [
"10372246",
"10372249",
"10372236",
"10372239",
"10372234",
"10372235",
"10372231",
"10372233",
"10377244",
"10377245",
"10377254",
"10377247",
"10377251",
"10377248",
"10377249",
"10377246",
],
"MarketCName": "win-draw-win",
"ScoreboardRequest": {"ScoreboardType": "3", "IncidentRequest": {}},
"BrowserId": "2",
"OsId": "4",
"ApplicationVersion": "",
"BrowserVersion": "15.4",
"OsVersion": "10.15.7",
"SessionId": "null",
"TerritoryId": "227",
"CorrelationId": "71a5a843-a9cd-4152-9ef5-cede7ee59e33",
"VisitId": "b0d95dbe-fc0c-482a-a45c-1e300c8682b9",
"ViewName": "sports",
"JourneyId": "77057d08-5dff-4da4-afd7-087b97024194",
}

最新更新