python - 如何使用 python 将记录或列表转换为 JSON 格式?
问题描述
我正在尝试使用 Python 将列表转换为 JSON。查询的结果如下所示:
输出:
[<Record r=<Relationship id=106 nodes=(<Node id=71 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '105', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'remove', 'sing': '', 'pl': '', 'title': 'BMProcessor', 'version': 'v6.0'}>, <Node id=59 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '93', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'A DataProcessor which represents a ML algorithm', 'sing': '', 'pl': '', 'title': 'LearningProcessor', 'version': 'v6.0'}>) type='subclass_of' properties={}> b=<Node id=59 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '93', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'A DataProcessor which represents a ML algorithm', 'sing': '', 'pl': '', 'title': 'LearningProcessor', 'version': 'v6.0'}> n=<Node id=71 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '105', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'remove', 'sing': '', 'pl': '', 'title': 'BMProcessor', 'version': 'v6.0'}>>]
功能 :
def runQuery(query):
pprint.pprint(connection.execute(query))
当我执行一个简单的json.dumps()
它返回TypeError: Object of type is not JSON serializable
我想打印一个 JSON 格式。我该怎么做?
解决方案
你可以得到result.data()
这是一本字典。
您还可以遍历游标中的记录并一一转换,并提取您想要的字段,这就是上面链接示例所做的 - 传递游标并使用blob['field']
语法
def serialize_genre(genre):
return {
'id': genre['id'],
'name': genre['name'],
}
推荐阅读
- python - 我如何阅读里面的文字
在蟒蛇?
- azure-active-directory - 如何在不需要管理员同意的情况下在两个应用程序之间授予 Azure AD 应用程序权限?
- html - 在 ASP.NET MVC Razor 视图中动态设置 HTML Div 元素的 ID
- laravel - 您好我使用 laravel 和 Vue js 我作者项目它工作正常但是这个错误?
- python - 我的输出不断重复自己?(Python)
- java - 如何在 macOS 的停靠栏顶部显示 Java Swing 窗口?
- typescript - 每次在输入子项上键入时,键盘都会不断关闭
- wordcloud2 - Wordcloud2:是否可以只显示出现 n 次的单词?
- stored-procedures - 在 Google BigQuery 中重命名存储过程
- google-sheets - 索引匹配和求和值