使用泽西的根元素

时间:2015-09-22 14:49:02

标签: java json eclipse jersey

我必须得到一个json格式的ArrayList,我缺少的是根元素,事实上我得到了这个json:

[{"age":27,"firstName":"Lorenzo","id":1,"lastName":"Volponi"},{"age":60,"firstName":"Alberto","id":2,"lastName":"Volponi"}...

我想我必须得到像

这样的东西
["contatti": {"age":27,"firstName":"Lorenzo","id":1,"lastName":"Volponi"},{"age":60,"firstName":"Alberto","id":2,"lastName":"Volponi"}...

我在eclipse上使用泽西,这是我的网络服务:

@Path("/jsonServices")
public class JerseyRestService {
public ArrayList<Student> Rubrica=new ArrayList<Student>();
    @GET
    @Path("/print/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Student> produceJSON( @PathParam("name") String name ) {
        setRubrica();

        ArrayList<Student> Risultati=new ArrayList<Student>();
        int cc=1;
        for(int i=0; i< Rubrica.size();i++){
            String nome=Rubrica.get(i).getFirstName();
            String cognome=Rubrica.get(i).getLastName();
            if(Rubrica.get(i).getFirstName().equals(name)|| Rubrica.get(i).getLastName().equals(name))
            {
                Risultati.add(Rubrica.get(i));
            }
        }   
        return Risultati;
    }
    @GET
    @Path("/print")
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Student> rubica() {
        setRubrica();
        return Rubrica;

    }
public void setRubrica(){
    Rubrica.add(new Student("Lorenzo","Volponi",27,1,"0712811157"));
    Rubrica.add(new Student("Alberto","Volponi",60,2,"0712811157")); ...

}
}

谢谢

0 个答案:

没有答案