在公司金融云中臺(tái)項(xiàng)目中,需要對(duì)接不同的資金方和資產(chǎn)方,對(duì)接的接口JSON報(bào)文形式也是各式各樣。經(jīng)常需要獲取某個(gè)json字段,不得不層層獲取json對(duì)象,非常不方便。
后來(lái)發(fā)現(xiàn)Ognl(Object Graph Navigation Language 對(duì)象導(dǎo)航圖語(yǔ)言)能夠通過EL表達(dá)式獲取對(duì)象的屬性值。根據(jù)這一特性,經(jīng)過封裝,輕松實(shí)現(xiàn)獲取JSON多層嵌套數(shù)據(jù)。
工具特點(diǎn)
1、方便獲取JSON多層嵌套數(shù)據(jù),無(wú)需層層獲取json對(duì)象
2、當(dāng)json字段值為空字符串時(shí),返回空字符串
3、當(dāng)json字段不存在時(shí),返回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對(duì)象圖導(dǎo)航語(yǔ)言,獲取JSON多層嵌套數(shù)據(jù)
* @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;
}
}
測(cè)試
JSON示例
{
"body": {
"accountInfo": {
"loanBankCode": "0308",
"repayBankProvince": "",
"loanBankProvince": "",
"loanPrivatePublicAccount": "01",
"loanTrusteeType": "B134003"
},
"contactInfo": [{
"relativeName": "王一",
"relativeCompanyAddress": "來(lái)廣營(yíng)",
"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\":\"來(lái)廣營(yíng)\",\"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數(shù)據(jù)放到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對(duì)象
String expression = "body.contactInfo";
Object value2 = Ognl.getValue(expression, new OgnlContext(), source);
System.out.println("body.contactInfo:"+value2);
} catch (Exception e) {
e.printStackTrace();
}
}
測(cè)試結(jié)果
#body.body.accountInfo.loanBankCode:0308
#body.body.contactInfo[0].relativeName:王一
#body.body.accountInfo.repayBankProvince:
#body.body.accountInfo.repayBankProvince1:null
body.contactInfo:[{"relativeName":"王一","relativeCompanyAddress":"來(lái)廣營(yíng)","relativeCode":"F1004","relativeMobile":"18612994747"},{"relativeName":"王二","relativeCode":"F1004","relativeMobile":"18612994848"}]