2017-03-20 4 views
0

Ich lese bereits verschiedene Beiträge über Python-Konvertierung von str zu dic, aber ich habe immer noch Probleme und ich kann meine str in Dictionary nicht konvertieren.Wie konvertiert man eine JSON-Zeichenkette nach Dictionary in Python?

das ist meine 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]"} 

Ich versuche, ein wenig aufzuräumen mit

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

Also habe ich zurück:

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

ich verschiedene Methoden bin versucht, :

import json 
json.loads(MY_STR) 

import ast 
ast.literal_eval(MY_STR) 

etc ...

aber alle von ihnen geben Sie mir eine Art von Fehler zurück, könnten Sie mir bitte helfen?

+0

Vielen Dank, es ist –

+0

die Klärung nur Dinge für weitere Leser, das Problem mit dieser hatte man mehrere Wörterbücher in deiner Zeichenfolge. Deshalb funktionieren json und ast nicht. Das Hinzufügen von Klammern um alles funktioniert, weil es alle Wörterbücher zu einer Liste zusammenfasst. – abccd

Antwort

3

Umgeben Sie Ihren Original-String mit eckigen Klammern es ein gültiges JSON-String zu machen:

import json 

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

vielen Dank, es hat sehr gut funktioniert;) –

Verwandte Themen