在公司金融云中台项目中,需要对接不同的资金方和资产方,对接的接口JSON报文形式也是各式各样。经常需要获取某个json字段,不得不层层获取json对象,非常不方便。
后来发现Ognl(Object Graph Navigation Language 对象导航图语言)能够通过EL表达式获取对象的属性值。根据这一特性,经过封装,轻松实现获取JSON多层嵌套数据。
工具特点
1、方便获取JSON多层嵌套数据,无需层层获取json对象
2、当json字段值为空字符串时,返回空字符串
3、当json字段不存在时,返回NULL
POM引入
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>2.3.2</version>
</dependency>
封装OGNL工具类
import com.alibaba.fastjson.JSONObject;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.junit.Test;
/**
* @program: java-deep
* @description 工具类:使用Ognl对象图导航语言,获取JSON多层嵌套数据
* @author: DONGSHILEI
* @create: 2020/5/20 16:26
**/
public class OgnlUtil {
private OgnlContext context;
public OgnlUtil context(OgnlContext context) {
this.context = context;
return this;
}
public static OgnlUtil build() {
return new OgnlUtil();
}
public OgnlUtil add(JSONObject source) {
return this.add(null, source);
}
public OgnlUtil add(String key, JSONObject source) {
if (this.context == null) {
this.context = new OgnlContext();
}
if (key == null) {
this.context.setRoot(source);
} else {
this.context.put(key, source);
}
return this;
}
public static Object getValue(OgnlContext context, String dsl) throws OgnlException {
Object mapValue = Ognl.getValue(dsl, context, context.getRoot());
return mapValue;
}
public Object getValue(String dsl) throws OgnlException {
Object mapValue = Ognl.getValue(dsl, this.context, this.context.getRoot());
return mapValue;
}
}
测试
JSON示例
{
"body": {
"accountInfo": {
"loanBankCode": "0308",
"repayBankProvince": "",
"loanBankProvince": "",
"loanPrivatePublicAccount": "01",
"loanTrusteeType": "B134003"
},
"contactInfo": [{
"relativeName": "王一",
"relativeCompanyAddress": "来广营",
"relativeCode": "F1004",
"relativeMobile": "18612994747"
}, {
"relativeName": "王二",
"relativeCode": "F1004",
"relativeMobile": "18612994848"
}],
"professionInfo": {
"incomeLevelCode": "B20202",
"professionCode": "F12326",
"incomeSourceCode": "B20305",
"duty": "B2910",
"yearIncome": 48000,
"industryCode": "B1036"
}
}
}
public static void main(String[] args) {
String jsonStr = "{\"body\":{\"accountInfo\":{\"loanBankCode\":\"0308\",\"repayBankProvince\":\"\",\"loanBankProvince\":\"\",\"loanPrivatePublicAccount\":\"01\",\"loanTrusteeType\":\"B134003\"},\"contactInfo\":[{\"relativeName\":\"王一\",\"relativeCompanyAddress\":\"来广营\",\"relativeCode\":\"F1004\",\"relativeMobile\":\"18612994747\"},{\"relativeName\":\"王二\",\"relativeCode\":\"F1004\",\"relativeMobile\":\"18612994848\"}],\"professionInfo\":{\"incomeLevelCode\":\"B20202\",\"professionCode\":\"F12326\",\"incomeSourceCode\":\"B20305\",\"duty\":\"B2910\",\"yearIncome\":48000,\"industryCode\":\"B1036\"}}}";
JSONObject source = JSONObject.parseObject(jsonStr);
try {
//将json数据放到context中,并获取特定的值
OgnlContext context = new OgnlContext();
OgnlUtil ognlUtil = OgnlUtil.build().context(context).add("body", source);
Object value = ognlUtil.getValue("#body.body.accountInfo.loanBankCode");
System.out.println("#body.body.accountInfo.loanBankCode:"+value);
// 获取JsonArray中值
Object value1 = ognlUtil.getValue("#body.body.contactInfo[0].relativeName");
System.out.println("#body.body.contactInfo[0].relativeName:"+value1);
//获取的value值为空字符串,则正常打印空字符串
Object value3 = ognlUtil.getValue("#body.body.accountInfo.repayBankProvince");
System.out.println(value3);
//获取的json key不存在,则打印null
Object value4 = ognlUtil.getValue("#body.body.accountInfo.repayBankProvince1");
System.out.println(value4);
//获取json对象
String expression = "body.contactInfo";
Object value2 = Ognl.getValue(expression, new OgnlContext(), source);
System.out.println("body.contactInfo:"+value2);
} catch (Exception e) {
e.printStackTrace();
}
}
测试结果
#body.body.accountInfo.loanBankCode:0308
#body.body.contactInfo[0].relativeName:王一
#body.body.accountInfo.repayBankProvince:
#body.body.accountInfo.repayBankProvince1:null
body.contactInfo:[{"relativeName":"王一","relativeCompanyAddress":"来广营","relativeCode":"F1004","relativeMobile":"18612994747"},{"relativeName":"王二","relativeCode":"F1004","relativeMobile":"18612994848"}]
网友评论