如何通过JSON获取特定格式的数据

发布于 2024-11-06 17:55:00 字数 1948 浏览 0 评论 0原文

以下是用于从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-11-13 17:55:00

是的。您应该构建 JSON,因为

 users
  {
   name:
   jid:
  }

您可能想要定义一个映射 JSON 的 Java 类。

public class MyUser{
  public String name;
  public String jid;
  public MyUser(String name, String jid){
    this.name=name;
    this.jid=jid;
  }
}

然后,只需将所有在线使用附加到

ArrayList<MyUser> mul = new ArrayList<MyUser>();
if (status == true) {
 ...
 ...
 for(RosterEntry r:entries) { 
  if(...){
         ... 
  } 
  else
  { 
    mul.add(new MyUser(r.getName(),r.getUser());
    count2++; 
  }
 }

response.setContentType("application/json");
response.getWriter().write(new Gson().toJSON(mul));
response.setCharacterEncoding("utf-8");
response.sendRedirect("Roster.jsp");

JavaScript 中的列表或其他内容中

...
$('<li>').text(rosterEntry.name +":"+rosterEntry.jid).appendTo($ul);
...

yes. you should construct your JSON as

 users
  {
   name:
   jid:
  }

You may want to define a Java class mapping the JSON.

public class MyUser{
  public String name;
  public String jid;
  public MyUser(String name, String jid){
    this.name=name;
    this.jid=jid;
  }
}

And then, just append all the online uses to a List or something

ArrayList<MyUser> mul = new ArrayList<MyUser>();
if (status == true) {
 ...
 ...
 for(RosterEntry r:entries) { 
  if(...){
         ... 
  } 
  else
  { 
    mul.add(new MyUser(r.getName(),r.getUser());
    count2++; 
  }
 }

response.setContentType("application/json");
response.getWriter().write(new Gson().toJSON(mul));
response.setCharacterEncoding("utf-8");
response.sendRedirect("Roster.jsp");

In your JavaScript

...
$('<li>').text(rosterEntry.name +":"+rosterEntry.jid).appendTo($ul);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文