1
继获得的数据是代码片段,用来从GTALK如何通过JSON特定格式
if (status == true) {
Roster roster =connection.getRoster();
Collection<RosterEntry> entries =roster.getEntries();
System.out.println(roster.getEntryCount());
int count1 = 0;
int count2 = 0;
for(RosterEntry r:entries)
{
Presence presence = roster.getPresence(r.getUser());
if(presence.getType() == Presence.Type.unavailable)
{
// System.out.println(user + "is offline");
count1++;
}
else
{
String rosterNamesJson = new Gson().toJson(r.getName());
String rosterUserJson =new Gson().toJson(r.getUser());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
//array of 2 elements
String rosterInfoJson=response.getWriter().write("rosterNamesJson"+"rosterUserJson"]);
response.sendRedirect("Roster.jsp");
//System.out.println(name+user + "is online");
count2++;
}
}
}
获得在线花名册现在,在我的jsp页面我想填充名册的名字和他jid ie xoxoxo:[email protected] jjj:[email protected]等。我如何实现这一目标?
我要建设的Json元素即
users
{
name:
jid:
}
,然后写在我的JSP页面的功能来访问数据?
我所拥有的功能是
$(function() {
$.getJSON('xxxServlet', function(rosterInfoJson) {
var $ul = $('<ul>').appendTo($('#roster'));
$.each(rosterInfoJson, function(index, rosterEntry) {
$('<li>').text(rosterEntry.user).appendTo($ul);
});
});
});
@ Nishanth,为什么我需要一个Java类??我我无法理解映射.. – enthusiastic 2011-05-11 05:10:11
@xoxoxo好,'新的Gson()。toJson(“某些字符串”)'给出''一些字符串“'。您需要打包为Java对象,因为您想创建一个与其完全相同的JSON对象。如果你不想使用Object,你也可以使用HashMap。 'HashMap hm = new HashMap (); hm.put(“name”,r.getName()); hm.put(“jid”,r.getUser()); mul.add(hm);'这会做同样的事情。 –
Nishant
2011-05-11 05:22:37