尝试使用JSON对象作为参数(Python)

2024-05-20 02:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一些代码,将JSON对象转换为iCalendar(.ics)对象,现在我正在尝试测试它。问题是我不知道如何创建一个通用的JSON对象来用作参数。我的一些尝试如下:

# 1
obj_json = u'sample json data in string form'
obj = json.loads(obj_json)

# 2
# I'm not sure about this very first line. My supervisor told me to put it in but he
# has a very heavy accent so I definitely could have heard him incorrectly.
input.json
with open('input.json') as f:
    obj = json.loads(f.read())

Tags: sample对象代码injsonobjinputdata
2条回答

看pnv的答案,但你可能不需要抛弃它。就像pnv一样,只要用一本字典,然后把它传给你需要的任何东西。除非您要将json对象通过连接传递给某个对象,否则我不知道您为什么要转储它。

我本想把这个作为评论,但还没有代表。:)

试试看

import json

some_dict = {'id': 0123, 'text': 'A dummy text'}

dummy_json = json.dumps(some_dict)

现在,将您的虚拟json馈送到您的函数。i、 e

'{"text": "A dummy text", "id": 83}'

也可以使用字符串对象进行转储。

相关问题 更多 >