在python中使用多级JSON/dict/Template进行字符串/JSON格式设置



我想用从第二个JSON映射的键替换1个JSON中的文本。这两个JSON都是动态的,第一个JSON可以修改为使用第二个JSON中的键。

text_to_replace = json.dumps([{"type": "data", "layerName": "THIS IS Mohit", "property": "Source Text", "expression": "time > 4 ? 'This is {groom.first_name}' : 'This is {groom.first_name}'", "composition": "01 Oliver"}, {"type": "data", "layerName": "THIS IS {bride.first_name}", "property": "Source Text", "expression": "time > 4 ? 'This is Aashi' : 'This is {bride.first_name}'", "composition": "02 Amelia"}, {"type": "data", "layerName": "January 2020", "property": "Source Text", "expression": "time > 4 ? '21st January 2021' : '21st January 2021'", "composition": "03 November"}, {"type": "data", "layerName": "JANUARY 2020", "property": "Source Text", "expression": "time > 4 ? '21st January 2021' : '21st January 2021'", "composition": "02 Date"}])


context = {'function_list': [
{'name': 'Xbbd', 'venue': 'XbxbnXnx', 'time': '06:00 AM', 'date': '19th April 2020',
'date_d_m_y_hyphen': '19-Apr-2020', 'timestamp': 1587234600.0, 'date_hindi': '19 अप्रैल 2020',
'date_hindi_A': 'रविवार', 'date_hindi_B': 'अप्रैल', 'effective_date': '19th Apr 2020',
'effective_day': 'Sunday', 'effective_time': '06:00 AM Onwards', 'effective_month': None}],
'primary': {'first_name': 'Bride Name', 'last_name': 'Gshs', 'fathers_name': 'Sbsb',
'mothers_name': 'Bsb', 'grand_fathers_name': 'Sbdb', 'grand_mothers_name': 'Sb',
'effective_name': 'Bride Name Gshs',
'effective_parents_message': 'Daughter of nSbsb & Bsb',
'effective_grand_parents_message': 'Grand Daughter of nSbdb & Sb'},
'secondary': {'first_name': 'Groom Name', 'last_name': 'Xbbs', 'fathers_name': 'Xbdb',
'mothers_name': 'Bdbd', 'grand_fathers_name': 'Xbxbnd', 'grand_mothers_name': 'Xbx',
'effective_name': 'Groom Name Xbbs',
'effective_parents_message': 'Son of nXbdb & Bdbd',
'effective_grand_parents_message': 'Grand Son of nXbxbnd & Xbx'},
'other_details': {'special_message': '', 'special_message2': '', 'card_for': 'bride',
'invitation_from': 'zbzb', 'first_function_name': 'Xbbd', 'venue': 'XbxbnXnx',
'date': '19th Apr 2020', 'date_a': 'Sun', 'date_A': 'Sunday', 'date_d': '19',
'date_b': 'Apr', 'date_B': 'April', 'date_Y': '2020', 'day_th': 'th',
'time': '06:00 AM', 'date_hindi': '19 अप्रैल 2020', 'date_hindi_A': 'रविवार',
'date_hindi_B': 'अप्रैल', 'effective_date': '19th Apr 2020',
'effective_day': 'Sunday', 'effective_time': '06:00 AM Onwards',
'effective_month': None},
'static_details': {'weds': 'weds'}
}

我尝试使用Template类使用python字符串格式,但它似乎不支持复杂的dict。

from string import Template
s = Template("$bride['name'] likes what")
temp_json = {'bride': {'name': 'ishita'}}
print('temp_dict', type(temp_json))
temp = s.safe_substitute(temp_json)
print('string', s)
print('string', temp)

它不起作用。关于我还可以尝试做什么,有什么建议吗。

def test(self):
from django.template import Context, Template
template = Template("My name is {{ my_name }}.")
context = Context({"my_name": "Ujjwal"})
string = template.render(context)
print('string', string)

我使用的是Django模板引擎。默认情况下,它支持复杂的JSON。

最新更新