1

I am using jersey with springboot. Below is my code and response i get.

Code:

@Component
@Path("/books")
public class BookController {
    @GET
    @Produces("application/json")
    public Map getAllBooks() {

        Map jsonObject = new HashMap<>();
        jsonObject.put(1,1);
        jsonObject.put("2","string2");
        return jsonObject;
    }
}

Response in chrome browser :

{"1":1,"2":"string2"}

As you can see, the first object's key is an integer but it shows as string in the browser. how to display the key as an integer in the browser.

user10692042
  • 63
  • 1
  • 6

1 Answers1

1

According to the specification, the key of a JSON object is always a string. Quoting the RFC:

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

So the answer to your question is: you can't.

Community
  • 1
  • 1
g00glen00b
  • 41,995
  • 13
  • 95
  • 133