Pretty-print a Map in Java

前端 未结 16 1182
傲寒
傲寒 2020-12-04 10:11

I am looking for a nice way to pretty-print a Map.

map.toString() gives me: {key1=value1, key2=value2, key3=value3}

I

16条回答
  •  佛祖请我去吃肉
    2020-12-04 10:23

    Simple and easy. Welcome to the JSON world. Using Google's Gson:

    new Gson().toJson(map)
    

    Example of map with 3 keys:

    {"array":[null,"Some string"],"just string":"Yo","number":999}
    

提交回复
热议问题