How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Gson can also be used to serialize arbitrarily complex objects.
Here is how you use it:
Gson
will automatically convert collections toJSON
arrays. Gson can serialize private fields and automatically ignores transient fields.If you don't really need HashMap then you can do something like that:
Output:
No need for Gson or JSON parsing libraries. Just using
new JSONObject(Map<String, JSONObject>).toString()
, e.g:Underscore-java library can convert hash map or array list to json and vice verse.
Code example:
You can use:
Caution: This will only work for a
Map<String, String>!
Other functions you can get from its documentation
http://stleary.github.io/JSON-java/index.html
I found another way to handle it.
Hope this helps.
Thanks.