在我的jsp应用程序中,我已经调用了一个Web服务,我从中获取json fromat中的数据,如下所示
获取json数据的代码是
String recv ="";
String recvbuff ="";
URL jsonpage = new URL("http://107.109.6.236:3000/api/tokens.json");
URLConnection urlcon = jsonpage.openConnection();
BufferedReader buffread = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
while ((recv = buffread.readLine()) != null)
recvbuff += recv;
buffread.close();
下面的是recvbuff输出。
[{"id":1,"session_id":"11611fba0cd57f2f0e62acf746d7f60b","link":"","created_at":"2013- 12-10T13:13:31.000Z","updated_at":"2013-12-10T13:13:31.000Z"},
{"id":2,"session_id":"848960680b00502fc3e4c9cf0652a5fe","link":"","created_at":"2013-12-10T13:13:31.000Z","updated_at":"2013-12-10T13:13:31.000Z"},
{"id":3,"session_id":"101155b2c4cef0034804ed9b6806422e","link":"","created_at":"2013-12-10T13:13:31.000Z","updated_at":"2013-12-10T13:13:31.000Z"},
{"id":4,"session_id":"ec1373a7bdd291f60266ab6f8445c23b","link":"","created_at":"2013-12-10T13:13:31.000Z","updated_at":"2013-12-10T13:13:31.000Z"},
{"id":5,"session_id":"3eb8b185f0cbd71ff004b30453c90f54","link":"","created_at":"2013-12-10T13:13:31.000Z","updated_at":"2013-12-10T13:13:31.000Z"}]
我有{strong> token_id ,例如3eb8b185f0cbd71ff004b30453c90f54
,它将与 session_id 之一匹配。如果与精确字符串不匹配,它将重定向到错误页面。
为此,我使用以下代码。
if(recvbuff.contains(token_id)){
out.println("<br>Hello <b>"+token_id+"</b>! done");
}else{
response.sendRedirect(redirectURL);
}
如何在jsp页面中获得完全匹配?
答案 0 :(得分:3)
尝试这样的事情(在scriplets中写作jsp页面):
String ar = "[{\"id\":1,\"session_id\":\"11611fba0cd57f2f0e62acf746d7f60b\",\"link\":\"\",\"created_at\":\"2013- 12-10T13:13:31.000Z\",\"updated_at\":\"2013-12-10T13:13:31.000Z\"}, \n"
+ "{\"id\":2,\"session_id\":\"848960680b00502fc3e4c9cf0652a5fe\",\"link\":\"\",\"created_at\":\"2013-12-10T13:13:31.000Z\",\"updated_at\":\"2013-12-10T13:13:31.000Z\"},\n"
+ "{\"id\":3,\"session_id\":\"101155b2c4cef0034804ed9b6806422e\",\"link\":\"\",\"created_at\":\"2013-12-10T13:13:31.000Z\",\"updated_at\":\"2013-12-10T13:13:31.000Z\"},\n"
+ "{\"id\":4,\"session_id\":\"ec1373a7bdd291f60266ab6f8445c23b\",\"link\":\"\",\"created_at\":\"2013-12-10T13:13:31.000Z\",\"updated_at\":\"2013-12-10T13:13:31.000Z\"},\n"
+ "{\"id\":5,\"session_id\":\"3eb8b185f0cbd71ff004b30453c90f54\",\"link\":\"\",\"created_at\":\"2013-12-10T13:13:31.000Z\",\"updated_at\":\"2013-12-10T13:13:31.000Z\"}]";
String token_id = "3eb8b185f0cbd71ff004b30453c90f54";
JSONArray jsonArray = new JSONArray(ar);
boolean match_found = false;
o:
for (int i = 0; i < jsonArray.length(); i++) {
if (token_id.equals(jsonArray.getJSONObject(i).getString("session_id"))) {
match_found = true;
break o;
}
}
if(match_found == true)
{
response.sendRedirect("error.jsp");
}