JSLT是一种完整的JSON查询和转换语言。git地址:https://github.com/schibsted/jslt
java中使用:
<dependency>
<groupId>com.schibsted.spt.data</groupId>
<artifactId>jslt</artifactId>
<version>0.1.11</version>
<scope>compile</scope>
</dependency>
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.schibsted.spt.data.jslt.Expression; import com.schibsted.spt.data.jslt.Parser; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.io.IOException; @Slf4j public class JsltUtil { /** * 转换json字符串(忽略异常,当异常时返回null) * @param jsltStr jslt表达式 * @param inputStr json字符串入参 * @return */ public static String jsonAdapt(String jsltStr, String inputStr) { try { return jsonAdapt(jsltStr, inputStr, true); } catch (Exception e) { e.printStackTrace(); log.error("jsonAdapt error jsltStr={},inputStr={}", jsltStr, inputStr, e); } return null; } /** * 转换json字符串 * @param jsltStr jslt表达式 * @param inputStr json字符串入参 * @param ignoreException 是否忽略异常 如果为ture,出现异常后返回null * @return */ public static String jsonAdapt(String jsltStr, String inputStr, boolean ignoreException) throws IOException { if (StringUtils.isBlank(jsltStr) || StringUtils.isBlank(inputStr)) { log.info("jsonAdapt param illegal"); return null; } try { ObjectMapper mapper = new ObjectMapper(); JsonNode input = mapper.readTree(inputStr); Expression jslt = Parser.compileString(jsltStr); JsonNode output = jslt.apply(input); return output.toString(); } catch (Exception e) { log.error("jsonAdapt error jsltStr={},inputStr={}", jsltStr, inputStr, e); if (!ignoreException) { throw e; } } return null; } }
// jslt表达式 // crowd 根据age进行if判断输出人群 { "name" :.name, "age" :.age, "crowd" : if (.age< 18 ) "未成年" else if (.age< 60 ) "成年人" else "老年人" } // 入参: { "name" : "nameTest" , "age" : 10 } // 结果: { "name" : "nameTest" , "age" : 10 , "crowd" : "未成年" } |
// jslt表达式 // * : . 匹配输入对象中的所有键,除了那些已经指定的,并将它们复制到输出对象中;如果有部分字段不想全拷贝使用 :* - bar, baz, quux : . // 注意一点, * : 这个语法必须在最后,否则会报错 子对象中,没效果 "extendMsg3":{*:.} 这个字段直接不输出(只有当对象名和入参的一样时,才能在嵌套中再用* ,eg:"extendMsg":{*:.} 是生效的) { "extendMsg1" :.extendMsg, "extendMsg2" :{ "married2" :.extendMsg.married}, *:. } // 入参: { "name" : "nameTest" , "age" : 10 , "extendMsg" :{ "sex" : "男" , "married" : false , "education" : "小学生" } } // 结果: { "extendMsg1" : { "sex" : "男" , "married" : false , "education" : "小学生" }, "extendMsg2" : { "married2" : false }, "name" : "nameTest" , "age" : 10 , "extendMsg" : { "sex" : "男" , "married" : false , "education" : "小学生" } } |
// 展开 // jslt表达式 { "sex" :.extendMsg.sex, "married" :.extendMsg.married, * - extendMsg:. } // 入参: { "name" : "nameTest" , "age" : 10 , "extendMsg" :{ "sex" : "男" , "married" : false , "education" : "小学生" } } // 结果: { "sex" : "男" , "married" : false , "name" : "nameTest" , "age" : 10 } // 缩进 // jslt表达式 { "name" :.name, "age" :.age, "extendMsg" :{ "sex" :.sex, "married" :.married, "education" :.education} } // 入参: { "name" : "nameTest" , "age" : 10 , "sex" : "男" , "married" : false , "education" : "小学生" } // 结果: { "name" : "nameTest" , "age" : 10 , "extendMsg" : { "sex" : "男" , "married" : false , "education" : "小学生" } } |
/** 把集合全部转换 */ // jslt表达式 [ for (.) {*:.}] // 入参: [ { "name" : "nameTest" , "age" : 10 , "sex" : "男" , "married" : false , "education" : "小学生" }, { "name" : "nameTest2" , "age" : 102 , "sex" : "女" , "married" : true , "education" : "女博士" } ] // 结果: // 输出和入参相同 /** 集合部分转换 */ // jslt表达式 [ for (.) { "name" :.name, "age" :.age, "test" : "testxxx" }] // 结果: [ { "name" : "nameTest" , "age" : 10 , "test" : "testxxx" }, { "name" : "nameTest2" , "age" : 102 , "test" : "testxxx" } ] |
/** 1.复杂组合=基础+集合 转换;2.haveNextPaga 举例变量计算 */ // jslt表达式 { "pageIndex" :.pageNo, "pageSize" :.pageSize, "total" :.total, "haveNextPaga" : if ((.total - .pageNo * .pageSize) > 0 ) true else false , "data" :[ for (.) {*:.}] } // 入参: { "pageNo" : 1 , "pageSize" : 2 , "total" : 100 , "list" :[ { "name" : "nameTest" , "age" : 10 , "sex" : "男" , "married" : false , "education" : "小学生" }, { "name" : "nameTest2" , "age" : 102 , "sex" : "女" , "married" : true , "education" : "女博士" } ] } // 结果: { "pageIndex" : 1 , "pageSize" : 2 , "total" : 100 , "haveNextPaga" : true , "data" : [ { "key" : "pageNo" , "value" : 1 }, { "key" : "pageSize" , "value" : 2 }, { "key" : "total" , "value" : 100 }, { "key" : "list" , "value" : [ { "name" : "nameTest" , "age" : 10 , "sex" : "男" , "married" : false , "education" : "小学生" }, { "name" : "nameTest2" , "age" : 102 , "sex" : "女" , "married" : true , "education" : "女博士" } ] } ] } |
// jslt表达式 { "data1" :.[ 0 ].name, "data2" :.[ 0 ].age} // 入参: [ { "name" : "nameTest" , "age" : 10 , "sex" : "男" , "married" : false , "education" : "小学生" }, { "name" : "nameTest2" , "age" : 102 , "sex" : "女" , "married" : true , "education" : "女博士" } ] // 结果:{ "data1" : "nameTest" , "data2" : 10 }
|
原文:https://www.cnblogs.com/taocong/p/14202651.html