Home >Java >javaTutorial >JSON to Java Map: Json-lib or Custom Parser – Which is Better?
Converting JSON to Map: Using Json-Lib or Writing a Custom Parser
If you wish to convert JSON data like the provided example into a Java Map, utilizing third-party libraries such as Json-lib is a recommended approach. However, for simple mappings as in this case, you might find it easier to employ readily available tools from the http://json.org website (Java section).
Using Jackson
Among the tools from json.org, Jackson (https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator) offers a straightforward solution:
Map<String, Object> result = new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);
In this code, JSON_SOURCE can represent a File, input stream, reader, or a JSON content String.
Additional Tips
While writing a custom parser might seem tempting for such a simple task, it is generally advised against due to the potential for error. Instead, leverage the available libraries and tools that are designed specifically for handling JSON data manipulation.
The above is the detailed content of JSON to Java Map: Json-lib or Custom Parser – Which is Better?. For more information, please follow other related articles on the PHP Chinese website!