JSON File Reading Example
This program demonstrates reading JSON files.
Problem Statement
Write a Java program that reads a JSON file and prints its contents.
Source Code
| 1 | import java.io.*; |
| 2 | import org.json.simple.*; |
| 3 | import org.json.simple.parser.*; |
| 4 | public class ReadJSONExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq Read JSON Example"); |
| 7 | try(FileReader reader=new FileReader("data.json")) { |
| 8 | JSONParser parser=new JSONParser(); |
| 9 | JSONObject obj=(JSONObject)parser.parse(reader); |
| 10 | System.out.println("Eduinq Name: "+obj.get("name")); |
| 11 | System.out.println("Eduinq Country: "+obj.get("country")); |
| 12 | } catch(Exception e) { |
| 13 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 14 | } |
| 15 | } |
| 16 | } |
Program Output
Eduinq Read JSON Example Eduinq Name: Arun Eduinq Country: India
Explanation
JSON reading uses JSONParser from json-simple. - Parses file into JSONObject. - Exception handling ensures safe parsing. - In enterprise systems, JSON reading is used for configuration and APIs. Conclusion: Shows reading JSON files.