2017-03-20 6 views
0

私はすでにstrからdicへのpython変換に関する別の記事を読んでいますが、私はまだ問題があり、strを辞書に変換できません。PythonでJSON文字列をDictionaryに変換するにはどうすればいいですか?

これは私の元の文字列です:私は別の方法をしようとしている

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

:だから私は戻った私は

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

で少し整理してみ

{"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]"} 

import json 
json.loads(MY_STR) 

import ast 
ast.literal_eval(MY_STR) 

など

しかし、それらのすべてが私に何らかのエラーを返します。あなたは私を助けてくれますか?

+0

おかげでそんなに、それだけでさらに読者のために何かを明確に –

+0

を働いていた、それに伴う問題は、あなたが複数の辞書を持っていましたあなたのひもの中に。だから、jsonとastの両方が動作しないのです。すべての辞書をリストにまとめているため、すべての括弧を追加すると機能します。 – abccd

答えて

3

その有効なJSON文字列にする角括弧を使用して、元の文字列を囲み:

import json 

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

ありがとう、それはとてもうまくいった;) –

関連する問題