如何在 Python 中将 JSON 字符串转换为字典?

2024-03-21

我已经阅读了有关从 str 到 dic 的 python 转换的不同帖子,但我仍然遇到问题,并且无法在字典中转换我的 str 。

这是我原来的字符串:

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

我试着整理一下

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

我正在尝试不同的方法:

import json
json.loads(MY_STR)

import ast
ast.literal_eval(MY_STR)

etc...

但他们都给我返回了某种错误,你能帮我吗?


将原始字符串包围起来方括号使其成为有效的 JSON 字符串:

import json

valid_json_string = "[" + your_string + "]"  # or "[{0}]".format(your_string)
data = json.loads(valid_json_string)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Python 中将 JSON 字符串转换为字典? 的相关文章

  • 如何将脚本作为 pytest 测试运行

    假设我有一个用简单脚本表示的测试assert 陈述 请参阅背景了解原因 例如 import foo assert foo 3 4 我如何以一种好的方式将该脚本包含在我的 pytest 测试套件中 我尝试了两种有效但不太好的方法 一种方法是将
  • 在 C++ 中查找精确的字符串匹配

    这是我用来检测 txt 文件中一行中的字符串的代码 int main std ifstream file C log txt std string line while file eof while std getline file lin
  • 如何在动态执行的代码字符串中使用inspect.getsource?

    如果我在文件中有这段代码 import inspect def sample p1 print p1 return 1 print inspect getsource sample 当我运行脚本时 它按预期工作 在最后一行 源代码sampl
  • 为什么该字符串的长度比其中的字符数长?

    这段代码 string a abc string b A C Console WriteLine Length a 0 a Length Console WriteLine Length b 0 b Length outputs Lengt
  • Karasuba算法递归过多

    我正在尝试用 c 实现 Karasuba 乘法算法 但现在我只是想让它在 python 中工作 这是我的代码 def mult x y b m if max x y lt b return x y bm pow b m x0 x bm x1
  • Objective-C 使用字符串池吗?

    我知道Java https stackoverflow com questions 3801343 what is string pool in java and C http msdn microsoft com en us librar
  • Django Web 应用程序中的 SMTP 问题

    我被要求向使用 Django Python 框架实现的现有程序添加一个功能 此功能将允许用户单击一个按钮 该按钮将显示一个小对话框 表单以输入值 我确实编写了一些代码 显示电子邮件已发送的消息 但实际上 它没有发送 My code from
  • 一个类似 dict 的 Python 类

    我想编写一个自定义类 其行为类似于dict 所以 我继承自dict 不过 我的问题是 我是否需要创建一个私有的dict我的成员 init 方法 我不明白这个有什么意义 因为我已经有了dict如果我只是继承自的行为dict 谁能指出为什么大多
  • Django 1.7 应用程序配置导入错误:没有名为 appname.apps 的模块

    我正在尝试按照以下文档为我的一个名为 文章 的 Django 应用程序设置自定义应用程序配置https docs djangoproject com en dev ref applications https docs djangoproj
  • Python:“直接”调用方法是否实例化对象?

    我是 Python 新手 在对我的对象进行单元测试时 我注意到一些 奇怪 的东西 class Ape object def init self print ooook def say self s print s def main Ape
  • 为什么在读取字典时要锁定

    我对我正在阅读的一本关于线程的书 C 3 in a Nutshell 中的代码列表感到困惑 在 应用程序服务器中的线程安全 主题中 给出了以下代码作为 UserCache 的示例 static class UserCache static
  • 更新 matplotlib 中颜色条的范围

    我想更新一个contourf在函数内绘制 效果很好 然而 数据的范围发生了变化 因此我还必须更新颜色条 这就是我未能做到的地方 请参阅以下最小工作示例 import matplotlib pyplot as plt import numpy
  • 在 for 循环中访问 itertools 产品的元素

    我有一个列表列表 是附加 itertools 产品的一些其他结果的结果 我想要的是能够使用 for 循环访问列表列表中列表的每个元素 但我无法访问所有元素 我只能访问最后一个列表的元素 结果是一个非常巨大的列表列表 例如 1 2 4 3 6
  • 重定向 python 交互式帮助()

    我正在为使用 Qt 的应用程序开发交互式 python shell 但是我似乎无法获得重定向的交互式帮助 我的 python 代码中有这个 class OutputCatcher def init self self data def wr
  • 如何从 PHP 中的字符串创建可能的字符串组合?

    如何从 PHP 中的字符串创建可能的字符串组合 Exp input abc output array 0 gt a 1 gt ab 2 gt abc 3 gt ac 4 gt acb 5 gt b 6 gt ba 7 gt bac 8 gt
  • 使用 Numpy 进行多维批量图像卷积

    在图像处理和分类网络中 一个常见的任务是输入图像与一些固定滤波器的卷积或互相关 例如 在卷积神经网络 CNN 中 这是一种极其常见的操作 我已将通用版本任务减少为 Given 一批 N 个图像 N H W D 和一组 K 个滤镜 K H W
  • 为什么我的 PyGame 应用程序根本不运行?

    我有一个简单的 Pygame 程序 usr bin env python import pygame from pygame locals import pygame init win pygame display set mode 400
  • python 日志记录替代方案 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 蟒蛇记录模块 http docs python org library logging html使用起来
  • 如何在 Qt 中以编程方式制作一条水平线

    我想弄清楚如何在 Qt 中制作一条水平线 这很容易在设计器中创建 但我想以编程方式创建一个 我已经做了一些谷歌搜索并查看了 ui 文件中的 xml 但无法弄清楚任何内容 ui 文件中的 xml 如下所示
  • PYTHON:从 txt 文件中删除 POS 标签

    我有以下 txt 文件 其中包含 POS 词性 http en wikipedia org wiki Part of speech tagging 每个单词的标签 不用 jj到 说 vb 我 ppss是 bedz愤怒 jj在 在 dt无与伦

随机推荐

  • 使用 BeautifulSoup 捕获 JavaScript 警报文本

    我正在使用这个 JavaScript 来验证表单
  • 将字符串附加到 Terraform 中列表的每个元素

    我的想法是通过向每个元素附加一个字符串来修改列表的元素 如何才能实现这一目标 我还没有找到任何功能可以让我做到这一点 你有没有尝试过格式列表 https www terraform io docs configuration interpo
  • Java:自动装箱和强制转换之间有什么区别?

    这个问题 https stackoverflow com questions 501412 why does autoboxing make some calls ambiguous in java是关于 为什么自动装箱会使 Java 中的
  • 免费的 C# XML Diff 库或类

    我正在寻找 XML Diff 类或库 我的要求有 开源 输出的对象模型 相当快 对于 4mb XML 我正在尝试使用 MS XML Diff 和修补工具 但我想获取具有 2 个 XML 文件 而不是 HTML 标记 差异的对象列表 UPD
  • Xamarin Android - 没有流程布局?

    我想以流程布局方式显示几个具有动态大小的小部件 但我似乎找不到流程布局 Xamarin for Android 中真的没有 flowlayout 作为框架的一部分吗 不 据我所知 Xamarin Android 中没有 flowlayout
  • 使用 ember 评估车把中的两个条件

    我想知道是否可以做这样的事情 if ClientController Client number PhoneController hasLinesToInstall if Thanks Juanitos 我认为不可能在车把中链接这样的条件
  • 真正的缩放是什么?

    我听说人们说他们已经制作了一个可扩展的 Web 应用程序 真正的缩放是什么 开发人员可以采取哪些措施来使其应用程序具有可扩展性 开发人员在扩展过程中会考虑哪些因素 有关使用 ASP NET 和 SQL Server 扩展 Web 应用程序的
  • 回购协议中的 TWIG 查询生成器

    我尝试将其放入referralentity php public function getTotalReferrals qb this gt createQueryBuilder r qb gt select COUNT r id AS t
  • 将语言名称转换为区域设置代码

    PHP 中是否有规范的方法来执行此操作 Java 问题 区域设置 语言名称到国家 语言代码 https stackoverflow com questions 20518000 locale language name to country
  • Android Studio:多模块项目中外部 JAR 的链接

    我有一个 Android 应用程序 在 Android Studio 上运行 它由 2 个模块组成 有一个低级纯java模块 我们称之为模块A 在它的顶部 有模块 B 它是 Android 应用程序 它依赖于 moduleA 来进行某些处理
  • 使用 PyParsing 解析 Snort 日志

    使用 pyparsing 模块解析 Snort 日志时遇到问题 问题在于分离 Snort 日志 它有多行条目 由空行分隔 并让 pyparsing 将每个条目解析为一个整体块 而不是逐行读取并期望语法适用于每一行 显然 它不是 我尝试将每个
  • 谷歌地图只显示半屏?

    谷歌地图只显示一半的屏幕 我尝试通过调整大小来修复 google maps event trigger map resize 但没有任何运气 有人可以帮忙吗 当它加载时 它只显示一半的屏幕 如下所示 我找啊找啊 没有任何解决办法 funct
  • 使用 AWS CLI 命令添加 SQS 重新驱动策略

    我尝试使用下面的 AWS CLI 命令设置 SQS 的重新驱动策略 但看到与重新驱动 JSON 相关的错误 您能让我知道如何解决这个问题吗 redrive policy RedrivePolicy deadLetterTargetArn d
  • 如何在Android Studio 2020中找到R文件?

    我在这里搜索了如何做到这一点 但我找到了 2015 年的源代码 并且 Android Studio 的布局从那时起就发生了变化 Android Studio 中的 R 文件在哪里 在此输入图像描述 https i stack imgur c
  • 缺少适用于 Windows 的远程 iOS 模拟器选项? [复制]

    这个问题在这里已经有答案了 我一直在使用适用于 Windows 的 Xamarin Remote iOS Simulator 和 Visual Studio Professional 版本 更新到最新版本的 Xamarin for Visu
  • 如何对 ksh 中的变量进行精确的单词匹配?

    我使用以下语法作为 ksh 脚本的一部分来验证该词是否Validation存在于LINE FROM FILE LINE FROM FILE Validation LINE FROM FILE print match Validation 这
  • 反应本机导航给出错误“未定义不是对象”?

    import Platform from react native import Navigation from react native navigation import registerScreens from screens reg
  • 使用扩展运算符和打字进行解构

    我有这个对象 const ABCD a 1 b 2 c 3 d 4 我可以对其进行解构 使用 扩展 运算符收集其余部分 然后输入如下变量 const a b restOfIt a number b number ABCD 但我该如何输入re
  • GitHub 如何在不重新加载页面的情况下更改 URL?

    转到任意 GitHubpage https github com twitter bootstrap单击任意目录 文件 观察 URL 如何变化 但仅更新部分页面 没有整个页面重新加载 我如何使用 jQuery 做类似的事情 这适用于大多数浏
  • 如何在 Python 中将 JSON 字符串转换为字典?

    我已经阅读了有关从 str 到 dic 的 python 转换的不同帖子 但我仍然遇到问题 并且无法在字典中转换我的 str 这是我原来的字符串 faqId 1 isPrivate false question Question 1 ans