我将保存在session对象中的jersey资源(java类)的JSON对象传递给jsp页面。
JSON:
{ "dbResult" : [{
"date" : "2016-05-27",
"firstName" : "ddddd",
"lastName" : "ccccc",
"status" : 19
},
{ "date" : "2016-05-29",
"firstName" : "aaaaa",
"lastName" : "bbbbb",
"status": 2
},
{ "date" : "2016-05-29",
"firstName" : "xxxxx",
"lastName" : "yyyyyy",
"status" : 94
}
]}
我试图解析json(在jsp中)文件并获取键/值对并显示它们......但我不断收到以下错误:
javax.servlet.ServletException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
我理解错误的含义,但所有其他尝试都无济于事。
JSP页面中的代码:
JSONArray jsArr = new JSONArray(session.getAttribute("obj").toString());
for (int j = 0; j < jsArr.length(); j++) {
JSONObject jsFinal = jsArr.getJSONObject(j);
Iterator<String> keys = jsFinal.keys();
while (keys.hasNext()) {
String key = keys.next();
System.out.println("Key :" + key + " Value :" + jsFinal.get(key));
}
}
非常感谢!