ERP解析外围系统json数据格式
外围系统调用ERP的WebService接口,将数据以json格式传到ERP,ERP解析json
1、创建java source jsp,提供java方法解析json数据
create or replace and compile java source named APPS.JsonUtil as import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import oracle.sql.CLOB; import java.sql.SQLException; import java.lang.Integer; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class JsonUtil{ //取json串单个节点值 public static String getValue(String jsonStr, String nodeName){ String nodeValue = ""; try{ if(jsonStr ==null||!jsonStr.startsWith("{")||!jsonStr.endsWith("}")){ nodeValue =""; }else{ JSONObject obj=new JSONObject(jsonStr); nodeValue =obj.getString(nodeName); } }catch(JSONException e){ nodeValue=""; } return nodeValue; } //取json数组长度便于循环处理 public static Integer getArrayLength(String jsonArrayStr){ Integer length=0; try { if(jsonArrayStr==null||!jsonArrayStr.startsWith("[")||!jsonArrayStr.endsWith("]")){ length=0; }else{ JSONArray jsonArr = new JSONArray(jsonArrayStr); length=jsonArr.length(); } } catch (JSONException e) { length=0; } return length; } //获取数组长度 public static String ClobToString(CLOB clob)throws SQLException, IOException { //CLOB clob = rs.getClob(1); ///String clobContent = clob.getSubString(1, (int) clob.length()); String reString = ""; Reader is = clob.getCharacterStream();// 得到流 BufferedReader br = new BufferedReader(is); String s = br.readLine(); StringBuffer sb = new StringBuffer(); while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING sb.append(s); s = br.readLine(); } reString = sb.toString(); return reString; } public static String getLabViewString(Integer i,CLOB clob)throws SQLException, IOException{ String LabViewStr = ClobToString(clob);//+","; String[] LabViewStr1 = LabViewStr.split("}"); String LabViewStr2 = LabViewStr1[i]; //Integer length = LabViewStr1.length; return LabViewStr2; } public static Integer getLabViewInteger(CLOB clob)throws SQLException, IOException{ String LabViewStr = ClobToString(clob);//+","; String[] LabViewStr1 = LabViewStr.split("}"); Integer length = LabViewStr1.length; return length; } //取json数组第index个元素 public static String getArrayValue(String jsonStr,Integer index){ String nodeValue=""; try { if(jsonStr==null||!jsonStr.startsWith("[")||!jsonStr.endsWith("]")){ nodeValue=""; }else{ JSONArray jsonArr = new JSONArray(jsonStr); nodeValue=jsonArr.getString(index); } } catch (JSONException e) { nodeValue=""; } return nodeValue; } }
在oracle中编译时,可以会报错缺少相关的类,需要导入json.jar
将json.jar上传到ERP数据库服务器JAVA_TOP下
在数据库服务器上,切换到JAVA_TOP目录下执行loadjava -r -f -u apps/apps@172.16.100.179:1533/TEST json.ja 导入json.jar
查看是否导入成功 SELECT * FROM user_java_classes t WHERE 1 = 1 AND t.name LIKE '%json%'
2、创建jsp和oracle 数据类型关联映射关系
CREATE OR REPLACE PACKAGE BODY cux_json_commm_util AS FUNCTION getval(jsonstr VARCHAR2, nodename VARCHAR2) RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'JsonUtil.getValue(java.lang.String,java.lang.String) return java.lang.String'; FUNCTION getarrval(jsonarraystr VARCHAR2, seqno NUMBER) RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'JsonUtil.getArrayValue(java.lang.String, java.lang.Integer) return java.lang.String'; FUNCTION getarrlen(jsonarraystr VARCHAR2) RETURN NUMBER AS LANGUAGE JAVA NAME 'JsonUtil.getArrayLength(java.lang.String) return java.lang.Integer'; /* FUNCTION getLabViewString(jsonarraystr clob) RETURN NUMBER AS LANGUAGE JAVA NAME 'JsonUtil.getArrayLength(java.lang.String) return java.lang.Integer';*/ FUNCTION getlabviewinteger(jsonclob CLOB) RETURN NUMBER AS LANGUAGE JAVA NAME 'JsonUtil.getLabViewInteger(oracle.sql.CLOB) return java.lang.Integer'; FUNCTION getlabviewstring(i NUMBER, jsonclob CLOB) RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'JsonUtil.getLabViewString(java.lang.Integer,oracle.sql.CLOB) return java.lang.String'; END cux_json_commm_util;r
3、解析json数据后进行处理
l_request_soap := p_request_soap; --l_request_soap := '[{"YPBH":"TEST20191020","SYDATE":"2019-10-20?09:39:07","JCDATE":"2019-10-21","SYDCJ":"宁乡一车间","SYDGXDM":"火法一次配料","SYDWL":"1505053900001","YPLY":"实验组","JCDD":"麓谷","YPGG":"108R","ZZFE":"1","ZZMG":"2","ZZCU":"3","ZZNA":"4","ZZCA":"5","ZZNI":"6","ZZMN":"7","ZZTI":"8","ZZAL":"9","ZZZR":"10","ZZCR":"11","ZZZN":"12","ZZCO":"13","ZZSI":"14","ZZS":"15","ZZP":"1.0","ZZPB":"1.1","ZZW":"1.2","ZZY":"1.3","ZZSR":"1.4","ZZCD":"1.5","ZZNB":"1.6","ZZK":"1.7","ZZB":"1.711","ZZLA":"2.34","ZZCE":"22","ZZMO":"12","ZZF":"8","ZZBA":"1","ZZV":"1","ZZLI":"1","CXWZFE":"1","CXWZNI":"1","CXWZZN":"1","CXWZCR":"1","CXWZHL":"1","ZLLI":"5","ZLNI":"8","ZLMN":"7","ZLCO":"6.6","ZLAL":"3.5","ZLPCO":"3.8","ZLLIM":"0.11","ZLNIM":"0.11","ZLMNM":"0.12","ZLCOM":"0.13","ZLALM":"0.14","A1":"0.15","DZHTCU":"0.16","DZHTZN":"0.17","NXAL":"0.1","NXCR":"0.1","NXCU":"0.1","NXFE":"0.1","NXZN":"0.1","NXYSAS":"0.1","NXYSSB":"0.1","NXNAOH":"0.1","NXNH3":"0.1"}]'; --获取labview传过来多少条数据 l_length := cux_json_commm_util.getlabviewinteger(l_request_soap); dbms_output.put_line('l_length:' || l_length); FOR i IN 0 .. l_length - 2 LOOP --获取从labview传过来的json字符串 l_return := cux_json_commm_util.getlabviewstring(i, l_request_soap); --dbms_output.put_line('l_return111111:' || l_return); --截取字符串,拼接成json格式{"name":"张三","age":"123"} l_return := substr(l_return, 2, length(l_return)) || '}'; dbms_output.put_line('json:' || l_return); --获取json数据指定元素的值 l_batch := cux_json_commm_util.getval(l_return, 'YPBH'); --批次 IF (l_batch IS NULL) THEN dbms_output.put_line('获取批次错误!'); l_error_msg := l_error_msg || '获取批次错误,请检查json'; x_return_status := 'E'; x_msg_data := x_msg_data || l_error_msg; continue; END IF; END LOOP;