【问题标题】:How to get data through JSON in a particular format如何通过 JSON 获取特定格式的数据
【发布时间】:2011-08-22 23:47:03
【问题描述】:

下面是代码sn-p,用于从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:xoxoxo@gmail.com jjj:jjj@gmail.com 等等。我如何做到这一点?

我应该构造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);
            });
        });
    });

【问题讨论】:

    标签: java json servlets jquery gson


    【解决方案1】:

    是的。您应该将 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);
    ...
    

    【讨论】:

    • @Nishanth-为什么我需要一个 java 类??我无法理解映射..
    • @xoxoxo 好吧,new Gson().toJson("Some String") 给了"Some String"。您需要打包为 Java 对象,因为您想创建一个与其完全相同的 JSON 对象。如果您不想使用 Object,也可以只使用 HashMap。 HashMap&lt;String, String&gt; hm = new HashMap&lt;String, String&gt;(); hm.put("name", r.getName()); hm.put("jid", r.getUser()); mul.add(hm); 这会做同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多