现在的位置: 首页 > 综合 > 正文

java中JSON数据的解析

2013年09月03日 ⁄ 综合 ⁄ 共 16901字 ⁄ 字号 评论关闭
2010-12-17

java中JSON数据的解析

文章分类:Java编程

Java代码 复制代码 收藏代码
  1. package com.sinoufc.base.realtimeMonitor;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.InputStreamReader;   
  5. import java.io.LineNumberReader;   
  6. import java.util.ArrayList;   
  7. import java.util.HashMap;   
  8. import java.util.List;   
  9. import java.util.Map;   
  10.   
  11. import com.sinoufc.base.org.json.JSONArray;   
  12. import com.sinoufc.base.org.json.JSONException;   
  13. import com.sinoufc.base.org.json.JSONObject;   
  14. import com.sinoufc.base.permission.baseinfo.dao.BaseInfoDao;   
  15. import com.sinoufc.framework.cache.Global;   
  16. import com.sinoufc.framework.exception.SystemException;   
  17.   
  18. public class ResolveJSONForShell {   
  19.     private  BaseInfoDao baseInfoDao=(BaseInfoDao) Global.getSpringBean("baseInfoDao");   
  20.        
  21.         public static List<String[]> resolveJSONForAllUsers(String ipaddress,String realtimePath) throws IOException{   
  22.             String shellvalue = executeShell(ipaddress,"host_all_users","",realtimePath);   
  23.             String ss="{'returncode': 0, 'response': [['qm', 'pts/0', '08:41', '38:09m', '2:16m', '0.58s', '-bash&#10;&#10;&#10;'],"  
  24.             +"['qm', 'pts/1', '10:07', '0.00s', '0.68s', '0.10s', 'java&#10;Shell&#10;192.&#10;'],"  
  25.             +"['qm', 'pts/2', '11:02', '3:43', '0.20s', '0.20s', '-bash&#10;&#10;&#10;'], "  
  26.             +"['qm', 'pts/3', '13:33', '49:54m', '1:21m', '0.02s', 'sshd:&#10;qm&#10;[priv]&#10;']], 'time:': 1289890011}";   
  27.             String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '', '']]}";   
  28.             if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  29.                 shellvalue=nullStr;   
  30.             }   
  31.             List<String[]> userlist=new ArrayList<String[]>();   
  32.              try{   
  33.                  JSONObject jsonObj = new JSONObject(shellvalue);   
  34.                  JSONArray array= jsonObj.getJSONArray("response");   
  35.                  for(int i=0;i<array.length();i++){   
  36.                      String[] alluserStr={(String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),   
  37.                              (String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),   
  38.                              (String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),   
  39.                              (String)array.getJSONArray(i).get(6)};   
  40.                      userlist.add(alluserStr);   
  41.                  }   
  42.             }catch(JSONException je){   
  43.                 je.printStackTrace();   
  44.                 throw new SystemException("连接不上主机或者没有数据!");   
  45.             }   
  46.             return userlist;   
  47.         }   
  48.        
  49.         public static List<String[]> resolveJSONForDiskSpaceWithTab(String ipaddress,String realtimePath)throws JSONException, IOException{   
  50.              String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);   
  51.              String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '']], "  
  52.                                       + "'time:': 1289891593}";   
  53.              if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  54.                     shellvalue=nullStr;   
  55.                 }   
  56.              List<String[]> diskSpacelist=new ArrayList<String[]>();   
  57.              try{   
  58.                  JSONObject jsonObj = new JSONObject(shellvalue);   
  59.                  JSONArray array= jsonObj.getJSONArray("response");   
  60.                    
  61.                  for(int i=0;i<array.length();i++){   
  62.                      String[] alluserStr={ (String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),   
  63.                              (String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),   
  64.                              (String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5)};   
  65.                      diskSpacelist.add(alluserStr);   
  66.                  }   
  67.                     
  68.             }catch(JSONException je){   
  69.                 je.printStackTrace();   
  70.                 throw new SystemException("连接不上主机或者没有数据!");   
  71.             }   
  72.             return diskSpacelist;   
  73.         }   
  74.            
  75.         public static List<String[]> resolveJSONForProcessesSnapshot(String ipaddress,String realtimePath)throws JSONException, IOException{   
  76.              String shellvalue = executeShell(ipaddress,"host_processes_snapshot","",realtimePath);   
  77.   
  78.             String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '', '', '', '', '', '']], 'time:': 1289891863}";   
  79.             if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  80.                     shellvalue=nullStr;   
  81.                 }   
  82.             List<String[]> processlist=new ArrayList<String[]>();   
  83.             try{   
  84.                  JSONObject jsonObj = new JSONObject(shellvalue);   
  85.                  JSONArray array= jsonObj.getJSONArray("response");   
  86.                     
  87.                  for(int i=0;i<array.length();i++){   
  88.                      String[] processStr={(String) array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),   
  89.                              (String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),   
  90.                              (String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),   
  91.                              (String)array.getJSONArray(i).get(6), (String)array.getJSONArray(i).get(7),   
  92.                              (String)array.getJSONArray(i).get(8), (String)array.getJSONArray(i).get(9),   
  93.                              (String)array.getJSONArray(i).get(10)};   
  94.                      processlist.add(processStr);   
  95.                  }   
  96.             }catch(JSONException je){   
  97.                 je.printStackTrace();   
  98.                 throw new SystemException("连接不上主机或者没有数据!");   
  99.             }   
  100.              return processlist;   
  101.                 
  102.         }   
  103.            
  104.         public static String[] resolveJSONForSwapSpace(String ipaddress,String realtimePath) throws JSONException, IOException{   
  105.             String shellvalue = executeShell(ipaddress,"host_swap_space","",realtimePath);   
  106.             System.out.println("----------shellvalue:"+shellvalue);   
  107.             String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289381310}";   
  108.             if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  109.                 shellvalue=nullStr;   
  110.             }   
  111.             String[] swapSpaceString = new String[3];   
  112.             try{   
  113.               JSONObject jsonObj = new JSONObject(shellvalue);   
  114.               JSONArray array= jsonObj.getJSONArray("response");   
  115.                  
  116.               System.out.println(array);   
  117.               System.out.println("used:"+(array.getJSONArray(0).get(1)));   
  118.               System.out.println("free:"+(array.getJSONArray(0).get(2)));   
  119.               swapSpaceString[0]=(String) array.getJSONArray(0).get(1);   
  120.               swapSpaceString[1]=(String)array.getJSONArray(0).get(2);   
  121.               swapSpaceString[2]=(String)array.getJSONArray(0).get(0);   
  122.             }catch(JSONException je){   
  123.                 je.printStackTrace();   
  124.                 throw new SystemException("连接不上主机或者没有数据!");   
  125.             }   
  126.               return swapSpaceString;   
  127.         }   
  128.         public static Map<String,String> resolveJSONForCPUPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{   
  129.             Map<String,String>  map =new HashMap<String,String>();   
  130.                
  131.             String shellvalue = executeShell(ipaddress,"host_cpu_performance","",realtimePath);   
  132.             String nullStr="{'returncode': 0, 'response': [['', '', '', '']], 'time:': 1289878737}";   
  133.             if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  134.                     shellvalue=nullStr;   
  135.                 }   
  136.             try{   
  137.              JSONObject jsonObj = new JSONObject(shellvalue);   
  138.              JSONArray array= jsonObj.getJSONArray("response");   
  139.              map.put("usertime", (String) array.getJSONArray(0).get(0));   
  140.              map.put("systemtime", (String) array.getJSONArray(0).get(1));   
  141.              map.put("wio", (String) array.getJSONArray(0).get(2));   
  142.              map.put("freetime", (String) array.getJSONArray(0).get(3));   
  143.             }catch(JSONException je){   
  144.                 je.printStackTrace();   
  145.                 throw new SystemException("连接不上主机或者没有数据!");   
  146.             }   
  147.             return map;   
  148.         }   
  149.         public static String resolveJSONForCPUUtilization(String ipaddress,String realtimePath) throws IOException{   
  150.         //  String jsonar ="["+randomNum()+"]";   
  151.             String shellvalue = executeShell(ipaddress,"host_cpu_performance","cpu_utilization",realtimePath);   
  152.             if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  153.                 shellvalue="[ ]";   
  154.             }   
  155.             System.out.println("resolveJSONForCPUUtilization:"+shellvalue);   
  156.             return shellvalue.substring(1,shellvalue.length()-1);   
  157.         }   
  158.         public static Map resolveJSONForDiskSpace(String ipaddress,String realtimePath) throws JSONException, IOException{   
  159.              String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);   
  160.              Map map=new HashMap();   
  161.              try{   
  162.                 List<String[]> diList=new ArrayList<String[]>();   
  163.                 List<Double[]> dataList=new ArrayList<Double[]>();   
  164.                 List<String> groupList=new ArrayList<String>();   
  165.                
  166.                 JSONObject jsonObj = new JSONObject(shellvalue);   
  167.                 JSONArray array= jsonObj.getJSONArray("response");   
  168.                    
  169.                 for(int i=0;i<array.length();i++){                  
  170.                     String[] arrays={(String) array.getJSONArray(i).get(1),   
  171.                             ((String) array.getJSONArray(i).get(2)),   
  172.                             ((String) array.getJSONArray(i).get(3)),   
  173.                             ((String) array.getJSONArray(i).get(5))};   
  174.                     diList.add(arrays);   
  175.                 }   
  176.                    
  177.                 for(String[] a: diList){   
  178.                        
  179.                     Double[] data={Double.parseDouble(a[0])/1024,Double.parseDouble(a[1])/1024,Double.parseDouble(a[2])/1024};   
  180.                     dataList.add(data);   
  181.                     groupList.add(a[3]);   
  182.                 }   
  183.                    
  184.                 double[][] dataArg=new double[3][dataList.size()];   
  185.                    
  186.                 for(int i=0;i<dataList.size();i++){   
  187.                        
  188.                     System.out.println((dataList.get(i))[0]);   
  189.                     System.out.println((dataList.get(i))[1]);   
  190.                     System.out.println((dataList.get(i))[2]);   
  191.                        
  192.                     dataArg[0][i]=(dataList.get(i))[0];   
  193.                     dataArg[1][i]=(dataList.get(i))[1];   
  194.                     dataArg[2][i]=(dataList.get(i))[2];   
  195.                 }   
  196.                 List rowList=new ArrayList();   
  197.                 rowList.add("all");   
  198.                 rowList.add("used");   
  199.                 rowList.add("free");   
  200.                    
  201.                    
  202.                 map.put("dataArg",dataArg);   
  203.                 map.put("groupList", groupList);   
  204.                 map.put("rowList", rowList);   
  205.              }catch(Exception e){   
  206.                  e.printStackTrace();   
  207.                  throw new SystemException("连接不上主机或者没有数据!");   
  208.              }   
  209.             return map;   
  210.         }   
  211.            
  212.            
  213.            
  214.         public static String[] resolveJSONForMemoryPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{   
  215.             String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);   
  216.              String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";   
  217.              if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  218.                     shellvalue=nullStr;   
  219.                 }   
  220.              String[] memoryString=new String[3];   
  221.              try{   
  222.               JSONObject jsonObj = new JSONObject(shellvalue);   
  223.               JSONArray array= jsonObj.getJSONArray("response");        
  224.                 memoryString[0]=(String)array.getJSONArray(0).get(0);   
  225.                 memoryString[1]=(String) array.getJSONArray(0).get(1);   
  226.                 memoryString[2]=(String)array.getJSONArray(0).get(2);   
  227.              }catch(JSONException je){   
  228.                     je.printStackTrace();   
  229.                     throw new SystemException("连接不上主机或者没有数据!");   
  230.                 }   
  231.             return memoryString;   
  232.         }   
  233.            
  234.         public static String[] resolveJSONForMemoryUsed(String ipaddress,String realtimePath) throws IOException, JSONException{   
  235.             String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);   
  236.              String[] memoryUsedStr=new String[2];   
  237.              String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";   
  238.              if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){   
  239.                     shellvalue=nullStr;   
  240.                 }   
  241.              try{   
  242.                  JSONObject jsonObj = new JSONObject(shellvalue);   
  243.                   JSONArray array= jsonObj.getJSONArray("response");   
  244.                   memoryUsedStr[0]=(String)array.getJSONArray(0).get(0);   
  245.                   memoryUsedStr[1]=(String) array.getJSONArray(0).get(1);   
  246.                   System.out.println("shellvalue"+shellvalue);   
  247.                 }catch(JSONException je){   
  248.                     je.printStackTrace();   
  249.                     throw new SystemException("连接不上主机或者没有数据!");   
  250.             }   
  251.             return memoryUsedStr;   
  252.         }   
  253.            
  254.            
  255.            
  256.            
  257.            
  258.   
  259.          public static String executeShell(String ip,String var1,String var2,String realtimePath) throws IOException {   
  260.              StringBuffer stringBuffer = new StringBuffer();   
  261.              String line="";   
  262.              InputStreamReader ir=null;   
  263.              LineNumberReader input=null;   
  264.                 
  265.             // HttpServletRequest request = (HttpServletRequest) ActionContext   
  266.             //.getContext().get(ServletActionContext.HTTP_REQUEST);   
  267.             // String root=request.getRealPath("/Shell");   
  268.            //  System.out.println("Reallllllllllllllpath:"+root);   
  269.              //String  filePath=null;   
  270.            /* try{  
  271.                 filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();  
  272.                 filePath =filePath.replace("WEB-INF/classes/", "");  
  273.              System.out.println("filePath@@@@@@@@@@@:"+filePath);  
  274.             }catch(Exception e){  
  275.                 e.printStackTrace();  
  276.             }*/  
  277.             // String path = httpServletRequest.getRealPath("/");   
  278.              try {   
  279.                             Process pid = null;   
  280.                             //String[] cmd = {"/bin/bash","-c","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ok   
  281.                           //String[] cmd = {"/bin/bash ","-c ","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng   
  282.                            //String[] cmd = {"/bin/bash","","./shell/test.sh "+ip+" "+var1+" "+var2};    ----ng   
  283.                             //String[] cmd = {"sh","-c","./shell/test.sh "+ip+" "+var1+" "+var2};        ----ng   
  284.                             String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();   
  285.                          filePath =filePath.replace("WEB-INF/classes/""");   
  286.                             String[] cmd = {"/bin/bash","-c",filePath+"shell/test.sh "+ip+" "+var1+" "+var2};   
  287.                             //String[] cmd = {"/bin/bash","-c","./shell/hello.sh "+ip+" "+var1+" "+var2};   
  288.                             System.out.println("****************"+cmd[0]+cmd[1]+cmd[2]);   
  289.                                     pid = Runtime.getRuntime().exec(cmd);   
  290.                                     if (pid != null) {   
  291.                                              ir=new InputStreamReader(pid.getInputStream());   
  292.                                              input = new LineNumberReader (ir);   
  293.                                               
  294.                                            line = input.readLine();   
  295.                                            System.out.println(line);   
  296.                                     }    
  297.                     } catch (Exception ioe) {   
  298.                             stringBuffer.append(ioe.getMessage());   
  299.                     }finally{   
  300.                         ir.close();   
  301.                         input.close();   
  302.                     }   
  303.               return line;   
  304.      }    
  305.       
  306.         private static int randomNum()      
  307.         {         
  308.             System.out.println((Math.random()*10000+50000));            
  309.             return (int)(Math.random()*10000+50000);      
  310.         }   
  311.     /*  public  static void  main(String[] arg) throws IOException{  
  312.              executeShell(arg[0],arg[1],arg[2]);  
  313.          }   */  
  314.   
  315. }

抱歉!评论已关闭.