如何在REST响应中以JSON Arrray格式检索结果

时间:2018-01-25 00:54:38

标签: java json web-services jersey

我正在尝试从数据库中检索数据并将它们作为JSON数组返回到“Response”中。 但是现在我在浏览器中得到的结果如下所示,这不是正确的JSON数组格式。如何以JSON数组的形式接收数据?

{"{\n  \"id\": 14,\n  \"name\": \"Test Doom Post\",\n  \"email\": \"test@test1.com\...

JDK 1.7 泽西岛(jaxrs-ri-2.25.1) Gson

//以下是我的Get方法:

@Path("/register")
public class JSONService {
    @GET
    @Path("/get")
    @Produces("application/json")
    @Consumes("application/json")
    public Response getRegisterInJSON() {

    JSONObject requestedJSON = new JSONObject();

    try {
        Class.forName("com.mysql.jdbc.Driver");
        SoccerUtils dbConnection = new SoccerUtils();
        Connection conn = dbConnection.getWeekendDBConnection();    

        PreparedStatement stmt = conn.prepareStatement("SELECT ID, FIRST_NAME, EMAIL FROM mycoolmap.weekendsoccer_login");
        ResultSet rs = stmt.executeQuery();
        while(rs.next())
        {
        RegisterPlayer playerObj = new RegisterPlayer();
            playerObj.setId(rs.getInt("ID"));
            playerObj.setName(rs.getString("FIRST_NAME"));
            playerObj.setEmail(rs.getString("EMAIL"));          

            Gson gson = new GsonBuilder().setPrettyPrinting().create();     
            String json1 = gson.toJson(playerObj);
            requestedJSON.put(json1, json1);            
            System.out.println(requestedJSON);                      

        }       



       } catch (Exception e) {
        e.printStackTrace();

       } finally {

       }

       return Response.status(Status.OK).entity(requestedJSON.toString()).build();


    }

//注册玩家POJO类:

@XmlRootElement
public class RegisterPlayer implements Serializable {
    private int id;
    private String name;
    private String email;   


    public RegisterPlayer() {

    }

    public RegisterPlayer(int id, String name, String email)
    {
    super();

    this.id =id;    
    this.name = name;
    this.email = email; 

    }

    public int getId()
    {
    return id;
    }

    public void setId(int id)
    {   
    this.id =id;        
    }
    public String getName()
    {
    return name;
    }

    public void setName(String name)
    {
    this.name = name;
    }

    public String getEmail()
    {
    return email;
    }

    public void setEmail(String email)
    {
    this.email = email;
    }


    @Override
    public String toString() {
        return "RegisterPlayer[id=" + id +", name=" + name +", email="+ email +"]";
    }



}

2 个答案:

答案 0 :(得分:1)

正如Roman在上面评论中所建议的那样,我创建了一个列表,添加了对象并返回列表。它按预期工作。

/Created a 'registerPlayerList'  List 
     List<RegisterPlayer> registerPlayerList = new ArrayList<RegisterPlayer>();
    //  Intialiaze the RegisterPlayer class
            RegisterPlayer playerObj = new RegisterPlayer();
    //set all the values into the object        
            playerObj.setId(rs.getInt("ID"));
            playerObj.setName(rs.getString("FIRST_NAME"));
            playerObj.setEmail(rs.getString("EMAIL"));
            ......
    //add the playerObj to the created registerPlayerList
            registerPlayerList.add(playerObj);

    // return the list      
            return registerPlayerList ;

答案 1 :(得分:0)

问题在于您将json打印到字符串(json1变量),但是您要将该字符串添加到JSONObject。当一个字符串被添加到JSONObject时,该字符串将被转义 - 这是一个String json对象。

如果您改为打印json1(并将其设置为实体),它应该有效。