为什么密钥显示为字符串?

Why the key is displaying as string?

我正在使用带有 springboot 的球衣。下面是我的代码和我得到的回复。

代码:

@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;
    }
}

chrome 浏览器中的响应:

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

如您所见,第一个对象的键是一个整数,但在浏览器中显示为字符串。如何在浏览器中将密钥显示为整数。

根据the specification,JSON对象的键总是一个字符串。引用 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.

所以你的问题的答案是:你不能。