How to convert hashmap to JSON object in Java

前端 未结 29 2516
谎友^
谎友^ 2020-11-22 11:20

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

29条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 11:44

    Gson can also be used to serialize arbitrarily complex objects.

    Here is how you use it:

    Gson gson = new Gson(); 
    String json = gson.toJson(myObject); 
    

    Gson will automatically convert collections to JSON arrays. Gson can serialize private fields and automatically ignores transient fields.

提交回复
热议问题