[英]Convert string dictionnary into dictionnary format
I try to convert a string value into Dictionary format.我尝试将字符串值转换为字典格式。 I must respect this syntax but when I try to convert the string value into a dictionary I have a error.
我必须尊重这种语法,但是当我尝试将字符串值转换为字典时出现错误。 The json.load convert the bracket nested into a string and not into a dictionary format.
json.load 将嵌套的括号转换为字符串而不是字典格式。
Do you have any idea to resolve this?你有什么想法来解决这个问题吗?
import json
dict_info = "{\"strategy\" : \"" + "ok" + "\", \"aa\" : \"" + "{\"strategy\" : \"" +
"strg" + "\"}" + "\"}"
json.loads(dict_info)
Thank you谢谢
This is because here you are specifying that the value for key aa
is a string by adding "
before and after the value,这是因为在这里您通过在值之前和之后添加
"
来指定键aa
的值是一个字符串,
ie this part: \" " + "{\"strategy\": \"" + "strg" + "\"}" + "
\"即这部分: \"
" + "{\"strategy\": \"" + "strg" + "\"}" + "
\"
If you remove those the code should work, here如果您删除那些代码应该可以工作,在这里
Code :代码:
import json
dict_info = "{\"strategy\" : \"" + "ok" + "\", \"aa\" : " + "{\"strategy\" : \"" + "strg" + "\"}" + "}"
d = json.loads(dict_info)
print(d)
print(d['aa'])
Output : Output :
{'strategy': 'ok', 'aa': {'strategy': 'strg'}}
{'strategy': 'strg'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.