JSONArray去重的问题
JSONArray array = ["2","0","0","0","0","0"];
private String[] instanceAaary(JSONArray array) throws JSONException{ String[] strs = new String[array.length()]; for (int i = 0; i < array.length(); i++) { strs[i] = array.getString(i); } List<String> result = new ArrayList<String>(); boolean flag; for(int i=0;i<strs.length;i++){ flag = false; for(int j=0;j<result.size();j++){ if(strs[i].equals(result.get(j))){ flag = true; break; } } if(!flag){ result.add(strs[i]); } } String[] arrayResult = result.toArray(new String[result.size()]); return arrayResult; }
输出结果 ["2","0"];
解析json在本地创建xml文件
public static final String CMD103 = "{\"CMD_ID\":103,\"CONTENT\":[[[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],1,\"00001\\u91c7\\u533a\"]," + "[[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],2,\"00002\\u91c7\\u533a\"]]}";
public void createAQDQ(Element root){ String[][] Data = null; //断电区域代码(根据煤矿井下区域划分情况设定,范围为0000-9999,不可重复。) String ss_poffarea_code = ""; // 断电区域名称 String ss_poffarea_name = ""; // 安全监控系统空间节点代码(空间节点代码不可重复) 暂时为空 String ss_gisnode_code = ""; try { //区域定义 JSONObject obj = new JSONObject(CMD103); if (obj.has("CONTENT")) { JSONArray Array1 = obj.getJSONArray("CONTENT"); //获取属性名对应的二维数组 Data = new String[Array1.length()][]; for (int i = 0; i < Array1.length(); i++) { JSONArray Array2 = Array1.getJSONArray(i); //获取一维数组 Data[i] = new String[Array2.length()]; for (int j = 0; j < Array2.length(); j++) { Data[i][j] = Array2.getString(j); //获取二维数组中的数据 //断电区域代码 ss_poffarea_code = Data[i][1]; // 断电区域名称 ss_poffarea_name = Data[i][2]; } Element element = new Element("data").setAttribute("ss_poffarea_code", ss_poffarea_code) .setAttribute("ss_poffarea_name", ss_poffarea_name); JSONArray Array3 = Array2.getJSONArray(0); for (int k = 0; k < instanceAaary(Array3).length; k++) { // 安全监控系统空间节点代码(空间节点代码不可重复) ss_gisnode_code = Array3.getString(k); element.addContent(new Element("point").setAttribute("ss_gisnode_code",ss_gisnode_code)); } root.addContent(element); } } }catch (JSONException e){ e.printStackTrace(); } }
等待30秒执行某方法
long lCurrentTime = System.currentTimeMillis(); if(!bStart106 && !CMD106.equals("")){ lLastTime = System.currentTimeMillis(); bStart106 = true; } if(bStart106 && ((lCurrentTime-lLastTime)>=30*1000)){ lLastTime = lCurrentTime; createXML("AQSS"); }
//数据生成时间 public String getCs_data_time(){ String[][] Data = null; String cs_data_time = ""; try{ JSONObject obj101 = new JSONObject(CMD101); if (obj101.has("CONTENT")){ JSONArray Array1 = obj101.getJSONArray("CONTENT"); //获取属性名对应的二维数组 Data = new String[Array1.length()][]; for (int i = 0; i < Array1.length(); i++) { JSONArray Array2 = Array1.getJSONArray(i); //获取CMD101一维数组 Data[i] = new String[Array2.length()]; for (int j = 0; j < Array2.length(); j++) { Data[i][j] = Array2.getString(j); //获取CMD101二维数组中的数据 //数据生成时间 cs_data_time = Data[i][0]; } } } }catch (JSONException e){ e.printStackTrace(); } return cs_data_time; }
//检测传感器状态 private String[] bitChannel(int ctbit){ String[] channel = new String[8]; for (int i = 0; i < channel.length; i++) { if((ctbit&0x1)==1){ channel[i]="1"; }else{ channel[i]="0"; } ctbit = ctbit>>1; } return channel; }
//去除重复 private String[] instanceAaary(JSONArray array) throws JSONException{ String[] strs = new String[array.length()]; for (int i = 0; i < array.length(); i++) { strs[i] = array.getString(i); } List<String> result = new ArrayList<String>(); boolean flag; for(int i=0;i<strs.length;i++){ flag = false; for(int j=0;j<result.size();j++){ if(strs[i].equals(result.get(j))){ flag = true; break; } } if(!flag){ result.add(strs[i]); } } String[] arrayResult = result.toArray(new String[result.size()]); return arrayResult; }
java代码获取websocket数据
package com.servlet; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.channels.NotYetConnectedException; import org.java_websocket.WebSocket.READYSTATE; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft_17; import org.java_websocket.handshake.ServerHandshake; public class Client { public static WebSocketClient client; public static void main(String[] args) throws URISyntaxException, NotYetConnectedException, UnsupportedEncodingException { client = new WebSocketClient(new URI("ws://192.168.126.131:9999"),new Draft_17()) { EstablishXMLFile exf = new EstablishXMLFile(); @Override public void onOpen(ServerHandshake arg0) { System.out.println("打开链接"); } @Override public void onMessage(String arg0) { exf.recieveMessage(arg0); } @Override public void onError(Exception arg0) { arg0.printStackTrace(); System.out.println("发生错误已关闭"); } @Override public void onClose(int arg0, String arg1, boolean arg2) { System.out.println("链接已关闭"); } @Override public void onMessage(ByteBuffer bytes) { try { System.out.println(new String(bytes.array(),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }; client.connect(); while(!client.getReadyState().equals(READYSTATE.OPEN)){ System.out.println("还没有打开"); } System.out.println("打开了"); send("hello world".getBytes("utf-8")); client.send("hello world"); } public static void send(byte[] bytes){ client.send(bytes); } }
使double型数据按某种格式输出
ss_analog_value = new DecimalFormat("00.0").format(Double.parseDouble("3.26"));
获取properties文件中filename或url
public String getPropertiesReader(String str){ Properties properties=new Properties();//获取Properties实例 InputStream inStream=getClass().getResourceAsStream("config.properties");//获取配置文件输入流 try { properties.load(inStream);//载入输入流 Enumeration enumeration=properties.propertyNames();//取得配置文件里所有的key值 while(enumeration.hasMoreElements()){ String key=(String) enumeration.nextElement(); if(str.equals("cs_mine_code")&&key.equals("cs_mine_code")){ return properties.getProperty(key); }else if(str.equals("filename")&&key.equals("filename")){ return properties.getProperty(key); }else if(str.equals("url")&&key.equals("url")){ return properties.getProperty(key); } } } catch (IOException e) { e.printStackTrace(); } return ""; }
获取txt配置文件(JSON数据格式)中的信息
private String getFormat(String type,String id,String str){ String ret = ""; try { File file = new File("kj66.txt"); InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader bufferedReader = new BufferedReader(read); StringBuffer buffer = new StringBuffer(); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { buffer.append(lineTxt); } JSONObject obj = new JSONObject(buffer.toString()); if(type.equals("simulate")){ //模拟量 JSONArray array_sim = obj.getJSONArray("simulate"); String[] sensorID_sim = new String[array_sim.length()]; String[] name_sim = new String[array_sim.length()]; String[] code_sim = new String[array_sim.length()]; String[] format_sim = new String[array_sim.length()]; String[] unit_sim = new String[array_sim.length()]; for(int i = 0; i < array_sim.length(); i++ ) { JSONObject json = array_sim.getJSONObject(i); sensorID_sim[i] = json.getString("sensorID"); name_sim[i] = json.getString("name"); code_sim[i] = json.getString("code"); format_sim[i] = json.getString("format"); unit_sim[i] = json.getString("unit"); if(sensorID_sim[i].equals(id)){ if(str.equals("name")){ return name_sim[i]; }else if(str.equals("code")){ return code_sim[i]; }else if(str.equals("format")){ return format_sim[i]; }else if(str.equals("unit")){ return unit_sim[i]; } } } } }catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return ""; }
********************hashmap巧妙取代for循环**************************
private HashMap<String, String[]> CMD101_Hash = new HashMap<String, String[]>(); private void InitCMD101() { try { if(CMD101.equals("")) return; JSONObject obj = new JSONObject(CMD101); if (obj.has("CONTENT")) { JSONArray array = obj.getJSONArray("CONTENT"); //获取属性名对应的二维数组 if(array.equals("")) return; for (int i = 0; i < array.length(); i++) { JSONArray array2 = array.getJSONArray(i); //获取CMD101一维数组 String[] data = new String[array2.length()]; for (int j = 0; j < array2.length(); j++) { data[j] = array2.getString(j); //获取CMD101二维数组中的数据 } String code = formatToThree(data[26]) + data[23] + formatToTwo(data[24]); CMD101_Hash.put(code, data); } } }catch (JSONException e){ e.printStackTrace(); } } private String[] getProperty_tp(String str26,String str23,String str24) { String code = formatToThree(str26) + str23 + formatToTwo(str24); return CMD101_Hash.get(code); }
hashmap解析
//新建一个hashmap往里面放入20个(key,value)对。 Iterator<Integer> iterator= (Iterator<Integer>) hashMap.keySet().iterator(); //获得keyset的iterator,进行遍历整个hashmap。 while(iterator.hasNext()) { Integer key=(Integer) iterator.next(); Integer val=(Integer)hashMap.get(key); System.out.println(key+": "+val); }
Oracle触发器
create or replace trigger tr_u_sub_station before update on sub_station for each rowdeclare -- local variables herebegin if updating('is_effect') then RAISE_APPLICATION_ERROR('-20000','不能更新is_effect字段'); end if; end tr_u_sub_station;