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

Trying to use a JSON object as a argument (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())

试试,

import json

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

dummy_json = json.dumps(some_dict)

现在,将你的虚拟对象 json 提供给你的函数。即

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

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

参见 pnv 的回答,但您可能不需要转储它。只需像 pnv 那样使用字典,然后将其传递给任何你需要的东西。除非您要通过网络将 json 对象传递给某物,否则我不知道您为什么要丢弃它。

我会把它添加为评论,但还没有代表。 :)