我有以下从servlet发送到索引页的String数据。
{ "hits" : {"hits" : [ { "_source":{"ID":"123","Status":"false","Name":"ABC_123","Date":"2010-08-16T11:07:48"} }, { "_source":{"ID":"124","Status":"false","Name":"ABC_678","Date":"2010-08-16T12:00:12"} }, { "_source":{"ID":"125","Status":"true","Name":"FGH_122","Date":"2010-08-16T12:01:48"} }, { "_source":{"ID":"126","Status":"false","Name":"TYT_333","Date":"2010-08-16T12:06:48"} }, { "_source":{"ID":"127","Status":"false","Name":"CVF_230","Date":"2010-08-16T12:07:18"} }, { "_source":{"ID":"128","Status":"true","Name":"AWE_101","Date":"2010-08-16T12:03:48"} }, { "_source":{"ID":"129","Status":"true","Name":"WEC_299","Date":"2010-08-16T12:07:29"} } ] }}
我想解析数据并以arraylist格式查看数据,如:
{ID:"123", "Name":"ABC_123"}
{ID:"124", "Name":"ABC_678"}
等...
我是如何从客户端或从服务器实现这一点的?请指教..谢谢
答案 0 :(得分:1)
创建一个新数组并在将它们返回到JSP之前向其中添加对象。
Array (
[0] => Array (
[studentID] => 1
[ParentID] => 1
)
[1] => Array (
[studentID] => 2
[ParentID] => 1
)
)
Array (
[0] => Array (
[studentID] => 5
[ParentID] => 1
)
[1] => Array (
[studentID] => 7
[ParentID] => 3
)
)
答案 1 :(得分:0)
您应该在循环之前创建ArrayList
HashMap
个,并在那里添加数据。
喜欢这个
...rest of your code...
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int i = 0 ; i < hitsArray.length(); i++) {
JSONObject jObject = hitsArray.getJSONObject(i);
Map<String, String> data = new HashMap<String, String>();
JSONObject source = jObject.get("_source");
Iterator<String> it = source.keys();
while (it.hasNext()) {
String key = it.next();
data.put(key, source.getString(key));
}
list.add(data);
}
request.setAttribute("jsonObject", list);
... rest of your code...
更新了jettison的代码,但我建议切换到javax.json;