背景:得到的接口返回的字符串中一個字符串類型的數據內還有object類型,如何解析成列在列表中?
接口返回數據:
{
"agentRunId":10697,
"applicationId":76,
"errorId":124,
"exceptionClass":"org.springframework.web.util.NestedServletException",
"message":"Request processing failed; nested exception is java.lang.NullPointerException",
"params":"{\"custom_params\":{\"http_status\":500,\"thread_name\":\"http-bio-8090-exec-6\"},\"request_referer\":\"http://localhost:8090/tpm/account/2/alerts/policyApp\",\"request_uri\":\"/tpm/account/2/dashboard/29/overview\",\"stack_trace\":[\"\\torg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)\",\"\\torg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)\",\"\\tjavax.servlet.http.HttpServlet.service(HttpServlet.java:620)\",\"\\tjavax.servlet.http.HttpServlet.service(HttpServlet.java:727)\",。。省略。。,\"\\tjava.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)\",\"\\tjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)\",\"\\tjava.lang.Thread.run(Thread.java:695)\"]}",
"path":"WebTransaction/SpringController/DashboardShowController/dashboardView",
"startTime":"2014-09-22 14:56:22",
"userId":0
}
"params":就是一個字符串類型,但是裏面包含很多Object
解析方法:
<span style="font-size:14px;">public class JsonDemo {
public static void main(String[] args) {
String data = sendGet("http://115.28.189.59/json.html", "utf-8");//接口地址,返回接口數據
JSONObject jsonObject = new JSONObject(data); //反序列化
// int angentRunId=jsonObject.getInt("agentRunId");
// int applicationId=jsonObject.getInt("applicationId");
// int errorId=jsonObject.getInt("errorId");
String exceptionClass=jsonObject.getString("exceptionClass");//excrption
String message=jsonObject.getString("message");//錯誤信息
String params = jsonObject.getString("params");//獲取params對應的字符串
String path = jsonObject.getString("path");//path
String startTime = jsonObject.getString("startTime");//時間
JSONObject paraJsonObject = new JSONObject(params);//params字符串
JSONObject cp = paraJsonObject.getJSONObject("custom_params");//獲取object數據,達到目的
//自定義參數
String[] names = JSONObject.getNames(cp);//Android使用:JSONArray names = defineJsonObject.names();
//自定義參數
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]+":"+cp.get(names[i]));
}
//請求參數
System.out.println("request_referer:"+paraJsonObject.get("request_referer"));
System.out.println("request_uri:"+paraJsonObject.get("request_uri"));
//stack traces
JSONArray trace = paraJsonObject.getJSONArray("stack_trace");
for (int i = 0; i < trace.length(); i++) {
System.out.println(trace.get(i));
}
}</span>
//獲取接口返回數據,方法不一
public static String sendGet(String url, String charset) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
// String userAgent = System.getProperty("http.agent");
// connection.setRequestProperty("user-agent", userAgent);
connection.setConnectTimeout(1000 * 10);
// 建立實際的連接
connection.connect();
// 定義 BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), charset));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送GET請求出現異常!" + e);
result = "";
e.printStackTrace();
}
// 使用finally塊來關閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
// System.out.println(result);
return result;
}
}