如何通过JSON获取特定格式的数据
以下是用于从 gtalk 获取在线名册的代码片段
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,即 xoxoxo:[电子邮件受保护] jjj:[电子邮件受保护] 等等。我该如何实现这一目标?
我应该构造 Json 元素 ie
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);
});
});
});
Following is the code snippet which is used to get the online roster from gtalk
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++;
}
}
}
Now in my jsp page I want to populate the roster as name and his jid i.e xoxoxo:[email protected]
jjj:[email protected] and so on. How do I achieve this?
Should I construct Json element i.e
users
{
name:
jid:
}
and then write function in my JSP page to access the data?
The function I have is
$(function() {
$.getJSON('xxxServlet', function(rosterInfoJson) {
var $ul = $('<ul>').appendTo($('#roster'));
$.each(rosterInfoJson, function(index, rosterEntry) {
$('<li>').text(rosterEntry.user).appendTo($ul);
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。您应该构建 JSON,因为
您可能想要定义一个映射 JSON 的 Java 类。
然后,只需将所有在线使用附加到
JavaScript 中的列表或其他内容中
yes. you should construct your JSON as
You may want to define a Java class mapping the JSON.
And then, just append all the online uses to a List or something
In your JavaScript