java中JSON数据的解析

package com.sinoufc.base.realtimeMonitor;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sinoufc.base.org.json.JSONArray;
import com.sinoufc.base.org.json.JSONException;
import com.sinoufc.base.org.json.JSONObject;
import com.sinoufc.base.permission.baseinfo.dao.BaseInfoDao;
import com.sinoufc.framework.cache.Global;
import com.sinoufc.framework.exception.SystemException;

public class ResolveJSONForShell {
private BaseInfoDao baseInfoDao=(BaseInfoDao) Global.getSpringBean("baseInfoDao");

public static List<String[]> resolveJSONForAllUsers(String ipaddress,String realtimePath) throws IOException{
String shellvalue = executeShell(ipaddress,"host_all_users","",realtimePath);
String ss="{'returncode': 0, 'response': [['qm', 'pts/0', '08:41', '38:09m', '2:16m', '0.58s', '-bash '],"
+"['qm', 'pts/1', '10:07', '0.00s', '0.68s', '0.10s', 'java Shell 192. '],"
+"['qm', 'pts/2', '11:02', '3:43', '0.20s', '0.20s', '-bash '], "
+"['qm', 'pts/3', '13:33', '49:54m', '1:21m', '0.02s', 'sshd: qm [priv]['', '', '', '', '', '', '']]}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
List<String[]> userlist=new ArrayList<String[]>();
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
for(int i=0;i<array.length();i++){
String[] alluserStr={(String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
(String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
(String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),
(String)array.getJSONArray(i).get(6)};
userlist.add(alluserStr);
}
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return userlist;
}

public static List<String[]> resolveJSONForDiskSpaceWithTab(String ipaddress,String realtimePath)throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);
String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '']], "
+ "'time:': 1289891593}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
List<String[]> diskSpacelist=new ArrayList<String[]>();
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

for(int i=0;i<array.length();i++){
String[] alluserStr={ (String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
(String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
(String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5)};
diskSpacelist.add(alluserStr);
}

}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return diskSpacelist;
}

public static List<String[]> resolveJSONForProcessesSnapshot(String ipaddress,String realtimePath)throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_processes_snapshot","",realtimePath);

String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '', '', '', '', '', '']], 'time:': 1289891863}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
List<String[]> processlist=new ArrayList<String[]>();
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

for(int i=0;i<array.length();i++){
String[] processStr={(String) array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
(String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
(String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),
(String)array.getJSONArray(i).get(6), (String)array.getJSONArray(i).get(7),
(String)array.getJSONArray(i).get(8), (String)array.getJSONArray(i).get(9),
(String)array.getJSONArray(i).get(10)};
processlist.add(processStr);
}
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return processlist;

}

public static String[] resolveJSONForSwapSpace(String ipaddress,String realtimePath) throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_swap_space","",realtimePath);
System.out.println("----------shellvalue:"+shellvalue);
String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289381310}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
String[] swapSpaceString = new String[3];
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

System.out.println(array);
System.out.println("used:"+(array.getJSONArray(0).get(1)));
System.out.println("free:"+(array.getJSONArray(0).get(2)));
swapSpaceString[0]=(String) array.getJSONArray(0).get(1);
swapSpaceString[1]=(String)array.getJSONArray(0).get(2);
swapSpaceString[2]=(String)array.getJSONArray(0).get(0);
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return swapSpaceString;
}
public static Map<String,String> resolveJSONForCPUPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{
Map<String,String> map =new HashMap<String,String>();

String shellvalue = executeShell(ipaddress,"host_cpu_performance","",realtimePath);
String nullStr="{'returncode': 0, 'response': [['', '', '', '']], 'time:': 1289878737}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
map.put("usertime", (String) array.getJSONArray(0).get(0));
map.put("systemtime", (String) array.getJSONArray(0).get(1));
map.put("wio", (String) array.getJSONArray(0).get(2));
map.put("freetime", (String) array.getJSONArray(0).get(3));
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return map;
}
public static String resolveJSONForCPUUtilization(String ipaddress,String realtimePath) throws IOException{
// String jsonar ="["+randomNum()+"]";
String shellvalue = executeShell(ipaddress,"host_cpu_performance","cpu_utilization",realtimePath);
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue="[ ]";
}
System.out.println("resolveJSONForCPUUtilization:"+shellvalue);
return shellvalue.substring(1,shellvalue.length()-1);
}
public static Map resolveJSONForDiskSpace(String ipaddress,String realtimePath) throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);
Map map=new HashMap();
try{
List<String[]> diList=new ArrayList<String[]>();
List<Double[]> dataList=new ArrayList<Double[]>();
List<String> groupList=new ArrayList<String>();

JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

for(int i=0;i<array.length();i++){
String[] arrays={(String) array.getJSONArray(i).get(1),
((String) array.getJSONArray(i).get(2)),
((String) array.getJSONArray(i).get(3)),
((String) array.getJSONArray(i).get(5))};
diList.add(arrays);
}

for(String[] a: diList){

Double[] data={Double.parseDouble(a[0])/1024,Double.parseDouble(a[1])/1024,Double.parseDouble(a[2])/1024};
dataList.add(data);
groupList.add(a[3]);
}

double[][] dataArg=new double[3][dataList.size()];

for(int i=0;i<dataList.size();i++){

System.out.println((dataList.get(i))[0]);
System.out.println((dataList.get(i))[1]);
System.out.println((dataList.get(i))[2]);

dataArg[0][i]=(dataList.get(i))[0];
dataArg[1][i]=(dataList.get(i))[1];
dataArg[2][i]=(dataList.get(i))[2];
}
List rowList=new ArrayList();
rowList.add("all");
rowList.add("used");
rowList.add("free");


map.put("dataArg",dataArg);
map.put("groupList", groupList);
map.put("rowList", rowList);
}catch(Exception e){
e.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return map;
}



public static String[] resolveJSONForMemoryPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{
String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);
String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
String[] memoryString=new String[3];
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
memoryString[0]=(String)array.getJSONArray(0).get(0);
memoryString[1]=(String) array.getJSONArray(0).get(1);
memoryString[2]=(String)array.getJSONArray(0).get(2);
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return memoryString;
}

public static String[] resolveJSONForMemoryUsed(String ipaddress,String realtimePath) throws IOException, JSONException{
String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);
String[] memoryUsedStr=new String[2];
String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
memoryUsedStr[0]=(String)array.getJSONArray(0).get(0);
memoryUsedStr[1]=(String) array.getJSONArray(0).get(1);
System.out.println("shellvalue"+shellvalue);
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return memoryUsedStr;
}






public static String executeShell(String ip,String var1,String var2,String realtimePath) throws IOException {
StringBuffer stringBuffer = new StringBuffer();
String line="";
InputStreamReader ir=null;
LineNumberReader input=null;

// HttpServletRequest request = (HttpServletRequest) ActionContext
//.getContext().get(ServletActionContext.HTTP_REQUEST);
// String root=request.getRealPath("/Shell");
// System.out.println("Reallllllllllllllpath:"+root);
//String filePath=null;
/* try{
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
System.out.println("filePath@@@@@@@@@@@:"+filePath);
}catch(Exception e){
e.printStackTrace();
}*/
// String path = httpServletRequest.getRealPath("/");
try {
Process pid = null;
//String[] cmd = {"/bin/bash","-c","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ok
//String[] cmd = {"/bin/bash ","-c ","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng
//String[] cmd = {"/bin/bash","","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng
//String[] cmd = {"sh","-c","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng
String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
String[] cmd = {"/bin/bash","-c",filePath+"shell/test.sh "+ip+" "+var1+" "+var2};
//String[] cmd = {"/bin/bash","-c","./shell/hello.sh "+ip+" "+var1+" "+var2};
System.out.println("****************"+cmd[0]+cmd[1]+cmd[2]);
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
ir=new InputStreamReader(pid.getInputStream());
input = new LineNumberReader (ir);

line = input.readLine();
System.out.println(line);
}
} catch (Exception ioe) {
stringBuffer.append(ioe.getMessage());
}finally{
ir.close();
input.close();
}
return line;
}

private static int randomNum()
{
System.out.println((Math.random()*10000+50000));
return (int)(Math.random()*10000+50000);
}
/* public static void main(String[] arg) th['', '', '', '', '', '', '']}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
List<String[]> userlist=new ArrayList<String[]>();
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
for(int i=0;i<array.length();i++){
String[] alluserStr={(String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
(String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
(String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),
(String)array.getJSONArray(i).get(6)};
userlist.add(alluserStr);
}
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return userlist;
}

public static List<String[]> resolveJSONForDiskSpaceWithTab(String ipaddress,String realtimePath)throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);
String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '']], "
+ "'time:': 1289891593}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
List<String[]> diskSpacelist=new ArrayList<String[]>();
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

for(int i=0;i<array.length();i++){
String[] alluserStr={ (String)array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
(String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
(String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5)};
diskSpacelist.add(alluserStr);
}

}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return diskSpacelist;
}

public static List<String[]> resolveJSONForProcessesSnapshot(String ipaddress,String realtimePath)throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_processes_snapshot","",realtimePath);

String nullStr="{'returncode': 0, 'response': [['', '', '', '', '', '', '', '', '', '', '']], 'time:': 1289891863}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
List<String[]> processlist=new ArrayList<String[]>();
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

for(int i=0;i<array.length();i++){
String[] processStr={(String) array.getJSONArray(i).get(0),(String) array.getJSONArray(i).get(1),
(String)array.getJSONArray(i).get(2),(String)array.getJSONArray(i).get(3),
(String)array.getJSONArray(i).get(4),(String)array.getJSONArray(i).get(5),
(String)array.getJSONArray(i).get(6), (String)array.getJSONArray(i).get(7),
(String)array.getJSONArray(i).get(8), (String)array.getJSONArray(i).get(9),
(String)array.getJSONArray(i).get(10)};
processlist.add(processStr);
}
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return processlist;

}

public static String[] resolveJSONForSwapSpace(String ipaddress,String realtimePath) throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_swap_space","",realtimePath);
System.out.println("----------shellvalue:"+shellvalue);
String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289381310}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
String[] swapSpaceString = new String[3];
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

System.out.println(array);
System.out.println("used:"+(array.getJSONArray(0).get(1)));
System.out.println("free:"+(array.getJSONArray(0).get(2)));
swapSpaceString[0]=(String) array.getJSONArray(0).get(1);
swapSpaceString[1]=(String)array.getJSONArray(0).get(2);
swapSpaceString[2]=(String)array.getJSONArray(0).get(0);
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return swapSpaceString;
}
public static Map<String,String> resolveJSONForCPUPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{
Map<String,String> map =new HashMap<String,String>();

String shellvalue = executeShell(ipaddress,"host_cpu_performance","",realtimePath);
String nullStr="{'returncode': 0, 'response': [['', '', '', '']], 'time:': 1289878737}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
map.put("usertime", (String) array.getJSONArray(0).get(0));
map.put("systemtime", (String) array.getJSONArray(0).get(1));
map.put("wio", (String) array.getJSONArray(0).get(2));
map.put("freetime", (String) array.getJSONArray(0).get(3));
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return map;
}
public static String resolveJSONForCPUUtilization(String ipaddress,String realtimePath) throws IOException{
// String jsonar ="["+randomNum()+"]";
String shellvalue = executeShell(ipaddress,"host_cpu_performance","cpu_utilization",realtimePath);
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue="[ ]";
}
System.out.println("resolveJSONForCPUUtilization:"+shellvalue);
return shellvalue.substring(1,shellvalue.length()-1);
}
public static Map resolveJSONForDiskSpace(String ipaddress,String realtimePath) throws JSONException, IOException{
String shellvalue = executeShell(ipaddress,"host_disk_space","",realtimePath);
Map map=new HashMap();
try{
List<String[]> diList=new ArrayList<String[]>();
List<Double[]> dataList=new ArrayList<Double[]>();
List<String> groupList=new ArrayList<String>();

JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");

for(int i=0;i<array.length();i++){
String[] arrays={(String) array.getJSONArray(i).get(1),
((String) array.getJSONArray(i).get(2)),
((String) array.getJSONArray(i).get(3)),
((String) array.getJSONArray(i).get(5))};
diList.add(arrays);
}

for(String[] a: diList){

Double[] data={Double.parseDouble(a[0])/1024,Double.parseDouble(a[1])/1024,Double.parseDouble(a[2])/1024};
dataList.add(data);
groupList.add(a[3]);
}

double[][] dataArg=new double[3][dataList.size()];

for(int i=0;i<dataList.size();i++){

System.out.println((dataList.get(i))[0]);
System.out.println((dataList.get(i))[1]);
System.out.println((dataList.get(i))[2]);

dataArg[0][i]=(dataList.get(i))[0];
dataArg[1][i]=(dataList.get(i))[1];
dataArg[2][i]=(dataList.get(i))[2];
}
List rowList=new ArrayList();
rowList.add("all");
rowList.add("used");
rowList.add("free");


map.put("dataArg",dataArg);
map.put("groupList", groupList);
map.put("rowList", rowList);
}catch(Exception e){
e.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return map;
}



public static String[] resolveJSONForMemoryPerformance(String ipaddress,String realtimePath) throws IOException, JSONException{
String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);
String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
String[] memoryString=new String[3];
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
memoryString[0]=(String)array.getJSONArray(0).get(0);
memoryString[1]=(String) array.getJSONArray(0).get(1);
memoryString[2]=(String)array.getJSONArray(0).get(2);
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return memoryString;
}

public static String[] resolveJSONForMemoryUsed(String ipaddress,String realtimePath) throws IOException, JSONException{
String shellvalue = executeShell(ipaddress,"host_memory_performance","",realtimePath);
String[] memoryUsedStr=new String[2];
String nullStr = "{'returncode': 0, 'response': [['', '', '']], 'time:': 1289891288}";
if((null==shellvalue)||shellvalue.equals("1")||shellvalue.equals("")){
shellvalue=nullStr;
}
try{
JSONObject jsonObj = new JSONObject(shellvalue);
JSONArray array= jsonObj.getJSONArray("response");
memoryUsedStr[0]=(String)array.getJSONArray(0).get(0);
memoryUsedStr[1]=(String) array.getJSONArray(0).get(1);
System.out.println("shellvalue"+shellvalue);
}catch(JSONException je){
je.printStackTrace();
throw new SystemException("连接不上主机或者没有数据!");
}
return memoryUsedStr;
}






public static String executeShell(String ip,String var1,String var2,String realtimePath) throws IOException {
StringBuffer stringBuffer = new StringBuffer();
String line="";
InputStreamReader ir=null;
LineNumberReader input=null;

// HttpServletRequest request = (HttpServletRequest) ActionContext
//.getContext().get(ServletActionContext.HTTP_REQUEST);
// String root=request.getRealPath("/Shell");
// System.out.println("Reallllllllllllllpath:"+root);
//String filePath=null;
/* try{
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
System.out.println("filePath@@@@@@@@@@@:"+filePath);
}catch(Exception e){
e.printStackTrace();
}*/
// String path = httpServletRequest.getRealPath("/");
try {
Process pid = null;
//String[] cmd = {"/bin/bash","-c","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ok
//String[] cmd = {"/bin/bash ","-c ","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng
//String[] cmd = {"/bin/bash","","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng
//String[] cmd = {"sh","-c","./shell/test.sh "+ip+" "+var1+" "+var2}; ----ng
String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
String[] cmd = {"/bin/bash","-c",filePath+"shell/test.sh "+ip+" "+var1+" "+var2};
//String[] cmd = {"/bin/bash","-c","./shell/hello.sh "+ip+" "+var1+" "+var2};
System.out.println("****************"+cmd[0]+cmd[1]+cmd[2]);
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
ir=new InputStreamReader(pid.getInputStream());
input = new LineNumberReader (ir);

line = input.readLine();
System.out.println(line);
}
} catch (Exception ioe) {
stringBuffer.append(ioe.getMessage());
}finally{
ir.close();
input.close();
}
return line;
}

private static int randomNum()
{
System.out.println((Math.random()*10000+50000));
return (int)(Math.random()*10000+50000);
}
/* public static void main(String[] arg) throws IOException{
executeShell(arg[0],arg[1],arg[2]);
} */

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java解析json字符串。 commons-beanutils-1.9.0 commons-collections-3.2.1 commons-lang-2.6 commons-logging-1.1.3 ezmorph-1.0.6 json-lib-2.4-jdk15 demo: package com; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.ezmorph.object.DateMorpher; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.JSONUtils; public class Jsontest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JSONObject jsonObj = new JSONObject(); jsonObj.put("name", "hzj"); jsonObj.put("sex", "female"); System.out.println(jsonObj); } public static Object jsonToBean(String jsonString, Class cla) { JSONObject jsonObj = null; try { setDateFormat2Java(); jsonObj = JSONObject.fromObject(jsonString); } catch (Exception ex) { ex.printStackTrace(); } return JSONObject.toBean(jsonObj, cla); } public static Object jsonToBean(String jsonString, Class cla, Map map) { JSONObject jsonObj = null; try { setDateFormat2Java(); jsonObj = JSONObject.fromObject(jsonString); } catch (Exception ex) { ex.printStackTrace(); } return JSONObject.toBean(jsonObj, cla, map); } public static Object[] jsonToArray(String jsonString, Class cla) { Object[] arrObj = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); arrObj = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); arrObj[i] = JSONObject.toBean(jsonObject, cla); } } catch (Exception ex) { ex.printStackTrace(); } return arrObj; } public static Object[] jsonToArray(String jsonString, Class cla, Map map) { Object[] arrObj = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); arrObj = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); arrObj[i] = JSONObject.toBean(jsonObject, cla, map); } } catch (Exception ex) { ex.printStackTrace(); } return arrObj; } public static List jsonToList(String jsonString, Class cla) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ArrayList(); for (Iterator iter = array.iterator(); iter.hasNext();) { JSONObject jsonObject = (JSONObject) iter.next(); list.add(JSONObject.toBean(jsonObject, cla)); } } catch (Exception ex) { ex.printStackTrace(); } return list; } public static List jsonToList(String jsonString, Class cla, Map map) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ArrayList(); for (Iterator iter = array.iterator(); iter.hasNext();) { JSONObject jsonObject = (JSONObject) iter.next(); list.add(JSONObject.toBean(jsonObject, cla, map)); } } catch (Exception ex) { ex.printStackTrace(); } return list; } public static Map jsonToMap(String jsonString) { Map map = null; try { setDateFormat2Java(); JSONObject jsonObject = JSONObject.fromObject(jsonString); map = new HashMap(); for (Iterator iter = jsonObject.keys(); iter.hasNext();) { String key = (String) iter.next(); map.put(key, jsonObject.get(key)); } } catch (Exception ex) { ex.printStackTrace(); } return map; } public static Object[] jsonToArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); } public static void setDateFormat2Java() { JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" })); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值