在简单的 json 字符串中打印所有项目的键和值到控制台

Print key and value of all items in simple json string to console

我有以下这些,我可以打印出每一个属性。很简单,但是有没有一种简单的方法来打印 JSON 字符串中每个项目的键和值?只是想在控制台中打印键和值。

private static void deserializeUserSimple() {
    String userJson = "{\"name\":\"smithy\",\"email\":\"blah@gmail.com\",\"age\":21,\"isDeveloper\":true}";

    Gson gson = new Gson();
    UserSimple userSimple = gson.fromJson(userJson, UserSimple.class);

     // this prints but looking for easy way to print all key and values
    System.out.println(userSimple.name);
}
String userJson = "{\"name\":\"smithy\",\"email\":\"blah@gmail.com\",\"age\":21,\"isDeveloper\":true}";
JsonObject convertedObject = new Gson().fromJson(userJson, JsonObject.class);
for(String key:convertedObject.keySet()){
            System.out.println("Key - " + key);
            System.out.println("Value - " + convertedObject.get(key));
}

输出:

Key - name
Value - "smithy"
Key - email
Value - "blah@gmail.com"
Key - age
Value - 21
Key - isDeveloper
Value - true