If you are using Java maven project below are steps.
1. Open pom.xml file and add below dependencies.
1 2 3 4 5 |
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> |
2. Create a java class and use below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package helper; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JSONReadFile { public static void main(String args[]) { JSONReadFile jsonReadFile = new JSONReadFile(); jsonReadFile.getJSONUsingFileName("json/example1.json"); } public void getJSONUsingFileName(String fileName) { String path = "src/test/resources/"; JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader(path+fileName)); JSONObject jsonObject = (JSONObject) obj; System.out.println(jsonObject); } catch (Exception e) { e.printStackTrace(); } } } |
3. Run the Java class.