Reading a JSON file Using Java
packagecom.gaurav.coreexamples;
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.IOException;
public classReadJsonObjectFromFileUsingJava {
public static voidmain(String... args) throws IOException {
BufferedReader reader = newBufferedReader(new FileReader(
"C:\\JsonData.json"));
StringBuilder sb = newStringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line + "\n");
line = reader.readLine();
}
reader.close();
String data = sb.toString();
System.out.println(data);
}
}
Result:-
Note:- Here i am reading a file named "JsonData.json" available in C:/ drive, in that file, below content is available:-
{"age":25,"name":"Gaurav","relations":["WIFE","FATHER","MOTHER","SISTER"]}
{"age":22,"name":"Kate","relations":["HUSBAND","MOTHER","BROTHER"]}
{"age":26,"name":"Julia","relations":["FATHER","MOTHER","SON"]}
Note:- Using this way, we can read any type of file and read any data available in buffer, we can also read HttpServletRequest data using the above code.
{"age":22,"name":"Kate","relations":["HUSBAND","MOTHER","BROTHER"]}
{"age":26,"name":"Julia","relations":["FATHER","MOTHER","SON"]}
Note:- Using this way, we can read any type of file and read any data available in buffer, we can also read HttpServletRequest data using the above code.
No comments:
Post a Comment