python Json递归渲染到html

我在尝试使用递归函数时遇到麻烦,因此可以将json文件转换为html渲染。

这是我的json。

{
"json": [
          {
            "type": "p",
            "children": [
              {
                "type": "text",
                "data": "Lorem Ipsum is simply dummy text "
              },
              {
                "type": "a",
                "attribs": {
                  "href": "http://example.com"
                },
                "children": [
                  {
                    "type": "text",
                    "data": "simply dummy text"
                  }
                ]
              },
              {
                "type": "text",
                "data": " Lorem Ipsum is simply dummy text 2"
              }
            ]
          },
          {
            "type": "p",
            "children": [
              {
                "type": "text",
                "data": "second Lorem Ipsum is simply dummy text"
              }
            ]
          },
          {
            "type": "div",
            "children": [
              {
                "type": "text",
                "data": "Third Lorem Ipsum is simply dummy text"
              }
            ]
          }
        ]
}

我需要一个嵌套函数,该函数将返回其子数据值的标签并从父标签呈现。

以下是我的输出:

<p>Lorem Ipsum is simply dummy text <a>simply dummy text</a> Lorem Ipsum is simply dummy text 2</p>
<p>second Lorem Ipsum is simply dummy text</p>
<div>Third Lorem Ipsum is simply dummy text</div>

我尝试了以下方法来获取数据,但对我没有帮助。

def json_render(body_data):
    allow_tags = [
        'p', 'div']
    cdata = '';
    for tag_type in body_data:

        tag = tag_type['type']    
        if tag_type['type'] in allow_tags:
            tag = tag_type['type']
            data = ""
            if tag_type['children']:
                data += child_data(tag_type['children'])
        cdata += f'<{tag}>' \
        f'{data}' \
        f'</{tag}>'

    return cdata

def child_data(data, name="",test=""):
    out = dict()
    if type(data) is dict:
        for a in data:
            child_data(data[a], f'{name}{a}_',"")
    elif type(data) is list:        
        i = 0
        for a in data:
            #test += parse_data(a)
            child_data(a, f'{name}{i}_',"")
            if a['type'] == 'text':
                if 'data' in a:    
                    test+=a["data"]
            i += 1
    return test 

这给我以下结果。它缺少锚文本。

<p>Lorem Ipsum is simply dummy text Lorem Ipsum is simply dummy text 2</p>
<p>second Lorem Ipsum is simply dummy text</p>
<div>Third Lorem Ipsum is simply dummy text</div>
评论
  • 冬莲
    冬莲 回复

    您可以使用递归:

    data = {'json': [{'type': 'p', 'children': [{'type': 'text', 'data': 'Lorem Ipsum is simply dummy text '}, {'type': 'a', 'attribs': {'href': 'http://example.com'}, 'children': [{'type': 'text', 'data': 'simply dummy text'}]}, {'type': 'text', 'data': ' Lorem Ipsum is simply dummy text 2'}]}, {'type': 'p', 'children': [{'type': 'text', 'data': 'second Lorem Ipsum is simply dummy text'}]}, {'type': 'div', 'children': [{'type': 'text', 'data': 'Third Lorem Ipsum is simply dummy text'}]}]}
    def render(d):
       return ''.join(i['data'] if i['type'] == 'text' else \
                f'<{i["type"]}>{render(i["children"])}</{i["type"]}>\n' for i in d)
    
    print(render(data['json']))
    

    输出:

    <p>Lorem Ipsum is simply dummy text <a>simply dummy text</a>
     Lorem Ipsum is simply dummy text 2</p>
    <p>second Lorem Ipsum is simply dummy text</p>
    <div>Third Lorem Ipsum is simply dummy text</div>