Fri, Nov 15, 2019
Read in 1 minutes
JSON -Javascript Object Notation. This can be parsed by the java API JSON.simple. For that, I have added json-simple-1.1.jar.
Download json-simple-1.1.jar
Add the jar file to your project.
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 JSONDemo {
public static JSONObject readJSON(){
JSONParser jsonParser = new JSONParser();
JSONObject departmentList = null;
try {
FileReader fread= new FileReader("/eclipse-workspace/JSONDemo/src/department.json");
Object obj = jsonParser.parse(fread);
departmentList = (JSONObject) obj;
// System.out.println(departmentList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return departmentList;
}
public static void main(String[] args) {
System.out.println(JSONDemo.readJSON());;
}
}
department.json
{
"department":{
"deptName":"ComputerScience",
"deptId":"101"
},
"subjects":{
"cs01":"C programming",
"cs02":"Java",
"cs03":"BigData",
"cs04":"datastructures and algorithms",
"cs05":"compiler design",
"cs06":"software design"
}
}