【问题标题】:How to handle the json convert object in python如何在python中处理json转换对象
【发布时间】:2019-04-12 01:13:43
【问题描述】:

我有一个 JSON 文件,我使用 json.load() 函数在 python 对象中转换它。我希望输出是 dict 或 list 但它是一个字符串。

PS:我无法分享的数据,因为它是生产数据。

提前致谢:)

【问题讨论】:

  • 请发布您的问题的一些代码/示例。
  • 您必须提供源代码和您的 json 结构,无论是审查值还是类似结构。

标签: python json parsing


【解决方案1】:

你的意思是你的文件是 myJsonfile.json 对吗? 并且你想将它们加载到 python 中?

你可以这样使用

import json
file = open('myjsonfile.json','r')
jsonfile = json.load(file)

检查数据是python现在调用

jsonfile

【讨论】:

  • 感谢您的回复。实际上,我有一个 JSON 文件,我使用上面提到的加载函数在 python 对象中转换了它。我得到的输出是一个字符串。我希望它是一个字典或列表。
  • 你能分享你的 json 文件的模型数据吗?我认为它与您的 json 文件有关。如果你得到像 "{'hahaha':'what what that'}" 这样的字符串,也许你可以 json.load() agian
【解决方案2】:

要从字符串更改为字典/列表,请使用json.loads()

那么你存储的每个字符串是什么,在下面我调用的示例中是jsonStr

jsonStr = '''{"file_type": "json" , "data_type": "str"}'''


jsonObj = json.loads(jsonStr)

输出:

print (jsonObj)
{'file_type': 'json', 'data_type': 'str'}

print (type(jsonObj))
<class 'dict'>

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 1970-01-01
    • 2013-12-12
    • 2012-02-25
    • 2020-05-27
    • 1970-01-01
    • 2020-04-08
    • 2011-09-28
    • 1970-01-01
    相关资源
    最近更新 更多