0

I am trying to retrieve the data from database and return them in 'Response' as JSON array. But now I am getting the result in browser as below, which is not the correct JSON array format. How can I receive the data as JSON Array ?

{"{\n  \"id\": 14,\n  \"name\": \"Test Doom Post\",\n  \"email\": \"[email protected]\...

JDK 1.7 Jersey (jaxrs-ri-2.25.1) Gson

//Following is my Get method below:

@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();


    }

// Register Player POJO class:

@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 +"]";
    }



}
3
  • 1
    Add objects to array to return array. Commented Jan 25, 2018 at 1:11
  • @RomanC: I did as per your comments and it worked ! Commented Jan 25, 2018 at 10:16
  • Great! You can answer your question if you know the difference between json object and Json array. Commented Jan 25, 2018 at 19:24

2 Answers 2

1

As advised by Roman in above comment, I have created a list, add the object and return the list. It worked as expected.

/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 ;
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you're printing the json to string (the json1 variable), but you're adding that string to a JSONObject. When a string is added to a JSONObject, the string will be escaped - that's a String json object.

If you print json1 instead (and set that as the entity), it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.