简体   繁体   English

如何在python中处理json转换对象

[英]How to handle the json convert object in python

I have a JSON file which I converted in the python object using json.load() function.I want the output to be dict or list but its a string.我有一个 JSON 文件,我使用 json.load() 函数在 python 对象中转换了它。我希望输出是 dict 或 list 但它是一个字符串。

PS: The data I could not share because its a production data. PS:我无法分享的数据,因为它是生产数据。

Thanks in advance:)提前致谢:)

you mean you have file is myJsonfile.json right ?你的意思是你的文件是 myJsonfile.json 对吗? and you want to load them into python?你想把它们加载到python中吗?

you can using like this你可以这样使用

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

to check data it's was python now call检查数据它是 python 现在调用

jsonfile

我的代码

to change from a string to dict/list use json.loads()要从字符串更改为 dict/list 使用json.loads()

So what every your stored string is, in the example below I called is jsonStr :所以你存储的每个字符串是什么,在下面的例子中我称之为jsonStr

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


jsonObj = json.loads(jsonStr)

Output:输出:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM