根據(jù)json模板對數(shù)據(jù)進行轉換

template-json

json template use json convert to json

主要功能: 根據(jù)模板對數(shù)據(jù)進行轉換
支持的語法: if、list、展開符“...”
數(shù)據(jù)轉換函數(shù): case 、 substring 、 dateformat

使用舉例子

引入jar


<dependency>
    <groupId>io.github.79121262</groupId>
    <artifactId>template-json</artifactId>
    <version>0.1.2</version>
</dependency>

例如需要轉換的數(shù)據(jù):

{
  "code": "XY202103240002",
  "parentAccount": "77120122000016235",
  "type": [
    1,
    2,
    3,
    4
  ],
  "parentBranch": "xxxxx科技支行 ZONG行",
  "childBankList": [
    {
      "bankBranchName": "xxxx科技支行",
      "companyName": "xxxx有限公司",
      "accountNumber": "77120122000016388",
      "createTime": "2021-03-24 13:56:12",
      "currency": "JPS",
      "id": "267",
      "address": [
        {
          "first": "海淀區(qū)",
          "second": "房山區(qū)"
        },
        {
          "first": "海淀區(qū)2",
          "second": "房山區(qū)3"
        }
      ]
    },
    {
      "bankBranchName": "XFAE 支行",
      "companyName": "xxxx有限公司1",
      "accountNumber": "77120122000016500",
      "createTime": "2021-03-24 13:56:12",
      "currency": "CNY",
      "id": "268",
      "address": [
        {
          "first": "平臺區(qū)",
          "second": "宣武區(qū)"
        },
        {
          "first": "平臺區(qū)2",
          "second": "宣武區(qū)3"
        }
      ]
    }
  ],
  "parentBank": {
    "bankBranchName": "xxxxx淀科技支行",
    "companyName": "xxxxx廣告有限公司",
    "accountNumber": "77120122000016235"
  }
}

定義模板:

{
  "x-if": {
    "expression": "1==1",
    "key": "re",
    "value": {
      "code": "${data.code}"
    }
  },
  "childBankList": {
    "reserved": "x-list",
    "expression": "item as data.childBankList",
    "value": {
      "companyName": "${item.bankBranchName}",
      "createTime": "${item.createTime}(dateformat[yyyy-MM-dd HH:mm:ss,yyyy-MM])"
    }
  },
  "accountNumbers": [
    {
      "reserved": "x-list",
      "expression": "item as data.childBankList",
      "value": [
        {
          "reserved": "x-list",
          "expression": "item2 as item.address",
          "value": {
            "first": "${item2.first}"
          }
        }
      ]
    }
  ],
  "type": [
    "...data.type"
  ],
  "${data.childBankList[1].companyName}": "${data.childBankList[1].currency}",
  "parentAccount": "${data.parentAccount}",
  "...data.parentBank": "..."
}

使用示例


public class ObjectTest {
    public static void main(String[] args) throws Exception {
        ClassLoader loader = ObjectTest.class.getClassLoader();
        byte[] tpl = Files.readAllBytes(Paths.get(loader.getResource("模板.json").toURI()));
        byte[] source = Files.readAllBytes(Paths.get(loader.getResource("需要轉換的數(shù)據(jù).json").toURI()));

        ObjectMapper mapper = new ObjectMapper();
        Object tempJson = mapper.readValue(tpl, Object.class);
        Object root = mapper.readValue(source, Object.class);
        Parse parse = new Parse(new DefaultExtractor(root));
        Object data = parse.parse(tempJson, root);

        //out put json
        System.out.println(mapper.writeValueAsString(data));

        //System.out.println(JSON.toJSONString("ABC"));
    }
}

輸出示例

{
  "accountNumbers": [
    {
      "first": "海淀區(qū)"
    },
    {
      "first": "海淀區(qū)2"
    },
    {
      "first": "平臺區(qū)"
    },
    {
      "first": "平臺區(qū)2"
    }
  ],
  "re": {
    "code": "XY202103240002"
  },
  "childBankList": [
    {
      "createTime": "2021-03",
      "companyName": "xxxx科技支行"
    },
    {
      "createTime": "2021-03",
      "companyName": "XFAE 支行"
    }
  ],
  "bankBranchName": "xxxxx淀科技支行",
  "companyName": "xxxxx廣告有限公司",
  "xxxx有限公司1": "CNY",
  "type": [
    1,
    2,
    3,
    4
  ],
  "parentAccount": "77120122000016235",
  "accountNumber": "77120122000016235"
}

github : https://github.com/79121262/template-json

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容