How convert a JSON string to Dictionary in Python?

让人想犯罪 __ 提交于 2021-02-08 15:05:59

问题


I already read different post regarding python conversion from str to dic but I still have problems and I can't convert my str in dictionary.

this is my original string:

{"faqId":1,"isPrivate":false,"question":"Question 1","answer":"# Hello world!!\r\n\r\n*   Proin elementum sollicitudin sodales.\r\n*   Nam id erat nec nibh dictum cursus.\r\n\r\n> In et urna eros. Fusce molestie, orci vel laoreet tempus, sem justo blandit magna, at volutpat velit lacus id turpis.  \r\n> Quisque malesuada sem at interdum congue. Aenean dapibus fermentum orci eu euismod.\r\n\r\n![](http://onehungrymind.com/wp-content/uploadspng)\r\n\r\n[This is an example link to nothing]()\r\n"},{"faqId":2,"isPrivate":false,"question":"Question 2","answer":"Dillinger\r\n=========\r\n\r\nDillinger is a cloud-enabled HTML5 Markdown editor.\r\n\r\n  - Type some Markdown text in the left window\r\n  - See the HTML in the right\r\n  - Magic\r\n\r\nMarkdown is a lightweight markup language based on the formatting conventions that people naturally use in email.  As [John Gruber] writes on the [Markdown site] [1]:\r\n\r\n> The overriding design goal for Markdown's\r\n> formatting syntax is to make it as readable \r\n> as possible. The idea is that a\r\n> Markdown-formatted document should be\r\n> publishable as-is, as plain text, without\r\n> looking like it's been marked up with tags\r\n> or formatting instructions.\r\n\r\nThis text you see here is *actually* written in Markdown! To get a feel for Markdown's syntax, type some text into the left window and watch the results in the right.  \r\n\r\nVersion\r\n----\r\n\r\n2.0\r\n\r\nTech\r\n-----------\r\n\r\nDillinger uses a number of open source projects to work properly:\r\n\r\n* [Ace Editor] - awesome web-based text editor\r\n* [Marked] - a super fast port of Markdown to JavaScript\r\n* [Twitter Bootstrap] - great UI boilerplate for modern web apps\r\n* [node.js] - evented I/O for the backend\r\n* [Express] - fast node.js network app framework [@tjholowaychuk]\r\n* [keymaster.js] - awesome keyboard handler lib by [@thomasfuchs]\r\n* [jQuery] - duh \r\n\r\nInstallation\r\n--------------\r\n\r\n```sh\r\ngit clone [git-repo-url] dillinger\r\ncd dillinger\r\nnpm i -d\r\nmkdir -p public/files/{md,html,pdf}\r\n```\r\n\r\n##### Configure Plugins. Instructions in following README.md files\r\n\r\n* plugins/dropbox/README.md\r\n* plugins/github/README.md\r\n* plugins/googledrive/README.md\r\n\r\n```sh\r\nnode app\r\n```\r\n\r\n\r\nLicense\r\n----\r\n\r\nMIT\r\n\r\n\r\n**Free Software, Hell Yeah!**\r\n\r\n[john gruber]:http://daringfireball.net/\r\n[@thomasfuchs]:http://twitter.com/thomasfuchs\r\n[1]:http://daringfireball.net/projects/markdown/\r\n[marked]:https://github.com/chjj/marked\r\n[Ace Editor]:http://ace.ajax.org\r\n[node.js]:http://nodejs.org\r\n[Twitter Bootstrap]:http://twitter.github.com/bootstrap/\r\n[keymaster.js]:https://github.com/madrobby/keymaster\r\n[jQuery]:http://jquery.com\r\n[@tjholowaychuk]:http://twitter.com/tjholowaychuk\r\n[express]:http://expressjs.com\r\n"},{"faqId":4,"isPrivate":false,"question":"Question 3","answer":"# Markdown Test\r\nThis is a link: [PDF link]"}

I try to tidy up a bit with

replace("false","False")
replace("true","True")
replace('"',"'")

So I got back:

{'faqId':1,'isPrivate':False,'question':'Question 1','answer':'# Hello world!!\r\n\r\n*   Proin elementum sollicitudin sodales.\r\n*   Nam id erat nec nibh dictum cursus.\r\n\r\n> In et urna eros. Fusce molestie, orci vel laoreet tempus, sem justo blandit magna, at volutpat velit lacus id turpis.  \r\n> Quisque malesuada sem at interdum congue. Aenean dapibus fermentum orci eu euismod.\r\n\r\n![](http://onehungrymind.com/wp-content/uploads.png)\r\n\r\n[This is an example link to nothing]()\r\n'},{'faqId':2,'isPrivate':False,'question':'Question 2','answer':'Dillinger\r\n=========\r\n\r\nDillinger is a cloud-enabled HTML5 Markdown editor.\r\n\r\n  - Type some Markdown text in the left window\r\n  - See the HTML in the right\r\n  - Magic\r\n\r\nMarkdown is a lightweight markup language based on the formatting conventions that people naturally use in email.  As [John Gruber] writes on the [Markdown site] [1]:\r\n\r\n> The overriding design goal for Markdown's\r\n> formatting syntax is to make it as readable \r\n> as possible. The idea is that a\r\n> Markdown-formatted document should be\r\n> publishable as-is, as plain text, without\r\n> looking like it's been marked up with tags\r\n> or formatting instructions.\r\n\r\nThis text you see here is *actually* written in Markdown! To get a feel for Markdown's syntax, type some text into the left window and watch the results in the right.  \r\n\r\nVersion\r\n----\r\n\r\n2.0\r\n\r\nTech\r\n-----------\r\n\r\nDillinger uses a number of open source projects to work properly:\r\n\r\n* [Ace Editor] - awesome web-based text editor\r\n* [Marked] - a super fast port of Markdown to JavaScript\r\n* [Twitter Bootstrap] - great UI boilerplate for modern web apps\r\n* [node.js] - evented I/O for the backend\r\n* [Express] - fast node.js network app framework [@tjholowaychuk]\r\n* [keymaster.js] - awesome keyboard handler lib by [@thomasfuchs]\r\n* [jQuery] - duh \r\n\r\nInstallation\r\n--------------\r\n\r\n```sh\r\ngit clone [git-repo-url] dillinger\r\ncd dillinger\r\nnpm i -d\r\nmkdir -p public/files/{md,html,pdf}\r\n```\r\n\r\n##### Configure Plugins. Instructions in following README.md files\r\n\r\n* plugins/dropbox/README.md\r\n* plugins/github/README.md\r\n* plugins/googledrive/README.md\r\n\r\n```sh\r\nnode app\r\n```\r\n\r\n\r\nLicense\r\n----\r\n\r\nMIT\r\n\r\n\r\n**Free Software, Hell Yeah!**\r\n\r\n[john gruber]:http://daringfireball.net/\r\n[@thomasfuchs]:http://twitter.com/thomasfuchs\r\n[1]:http://daringfireball.net/projects/markdown/\r\n[marked]:https://github.com/chjj/marked\r\n[Ace Editor]:http://ace.ajax.org\r\n[node.js]:http://nodejs.org\r\n[Twitter Bootstrap]:http://twitter.github.com/bootstrap/\r\n[keymaster.js]:https://github.com/madrobby/keymaster\r\n[jQuery]:http://jquery.com\r\n[@tjholowaychuk]:http://twitter.com/tjholowaychuk\r\n[express]:http://expressjs.com\r\n'},{'faqId':4,'isPrivate':False,'question':'Question 3','answer':'# Markdown Test\r\nThis is a link: [PDF link]'}

I'm trying different methods:

import json
json.loads(MY_STR)

import ast
ast.literal_eval(MY_STR)

etc...

but all of them give me back some kind of error, could you please help me?


回答1:


Surround your original string with square brackets to make it a valid JSON string:

import json

valid_json_string = "[" + your_string + "]"  # or "[{0}]".format(your_string)
data = json.loads(valid_json_string)


来源:https://stackoverflow.com/questions/42906591/how-convert-a-json-string-to-dictionary-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!