Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过JSON以特定格式获取数据_Java_Json_Servlets_Jquery_Gson - Fatal编程技术网

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

Java 如何通过JSON以特定格式获取数据,java,json,servlets,jquery,gson,Java,Json,Servlets,Jquery,Gson,下面是用于从gtalk获取在线花名册的代码片段 if (status == true) { Roster roster =connection.getRoster(); Collection<RosterEntry> entries =roster.getEntries(); System.out.println(roster.getEntryCount()); i

下面是用于从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页面中编写函数来访问数据

我的功能是

     $(function() {
        $.getJSON('xxxServlet', function(rosterInfoJson) {
            var $ul = $('<ul>').appendTo($('#roster'));
            $.each(rosterInfoJson, function(index, rosterEntry) {
                $('<li>').text(rosterEntry.user).appendTo($ul);
            });
        });
    });
$(函数(){
$.getJSON('xxxServlet',函数(rosterInfoJson){
变量$ul=$(“
    ”)。附录($(“#花名册”); $.each(rosterInfoJson,函数(索引,rosterEntry){ $(“
  • ”).text(rosterEntry.user).appendTo($ul); }); }); });
是。您应该将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");
ArrayList mul=new ArrayList();
如果(状态==真){
...
...
对于(名册r:条目){
如果(…){
... 
} 
其他的
{ 
mul.add(新的MyUser(r.getName(),r.getUser());
count2++;
}
}
setContentType(“应用程序/json”);
response.getWriter().write(新的Gson().toJSON(mul));
响应。setCharacterEncoding(“utf-8”);
response.sendRedirect(“花名册.jsp”);
在JavaScript中

...
$('<li>').text(rosterEntry.name +":"+rosterEntry.jid).appendTo($ul);
...
。。。
$(“
  • ”).text(rosternetry.name+”:“+rosternetry.jid).appendTo($ul); ...
  • @Nishanth为什么我需要一个java类?我不能很好地理解映射…@xoxo,
    new Gson().toJson(“一些字符串”)
    给出了
    “一些字符串”
    。您需要将其打包为Java对象,因为您想要创建一个与之完全相同的JSON对象副本。如果您不想使用对象,您也可以使用HashMap。
    HashMap hm=new HashMap();hm.put(“name”,r.getName());hm.put(“jid”,r.getUser());mul.add(hm);
    这也会做同样的事情。
    ...
    $('<li>').text(rosterEntry.name +":"+rosterEntry.jid).appendTo($ul);
    ...