zoukankan      html  css  js  c++  java
  • jinja2 中的 Template 批量替换json字符串中的内容

    项目中用到elasticsearch,使用Json格式查询方式,一个查询语句中有好几个地方需要替换,且替换的值都相同。最开始把json转为字符串发方式,利用format函数处理,发现再转回json时无法成功,同事提醒可以用jinja2模板处理字符串,

    second_search = """

        {
    "query": {
    "bool": {
    "should": [
    {
    "bool": {
    "should": [
    {
    "term": {
    "{{ prod }}_name_en.text": {
    "value": "{{ keyword }}",
    "boost": 10
    }
    }
    },
    {
    "term": {
    "{{prod}}_name_zh.text": {
    "value": "{{ keyword }}",
    "boost": 10
    }
    }
    }
    ]
    }
    },
    {
    "bool": {
    "should": [
    {
    "wildcard": {
    "{{prod}}_name_en.text": {
    "value": "{{ keyword }}*",
    "boost": 5
    }
    }
    },
    {
    "wildcard": {
    "{{prod}}_name_zh.text": {
    "value": "{{ keyword }}*",
    "boost": 5
    }
    }
    }
    ]
    }
    },
    {
    "query_string": {
    "fields": ["{{prod}}_name_en", "{{prod}}_name_zh"],
    "query": "{{ keyword }}",
    "boost": 1
    }
    }
    ]
    }
    }
    }
    """
    second_tpl = Template(second_search)
    second_str = second_tpl.render(keyword=kws, prod='other')
    second_json = json.loads(second_str)
    经过这么处理后再把字符串转为json对象很顺利
  • 相关阅读:
    IOS开发之——绘图(CGContext)
    网站相关人员信息记录humans.txt
    统一代码风格工具——editorConfig
    Bootstrap自动定位浮标
    Bootstrap图片轮播
    Bootstrap手风琴效果
    Bootstrap按钮插件
    Bootstrap弹出框
    Bootstrap提示框
    Bootstrap选项卡
  • 原文地址:https://www.cnblogs.com/fly-kaka/p/8989901.html
Copyright © 2011-2022 走看看