Java – read JSON file – Multiple Records

6
2



#ReadJSON #JavaReadJSON #JavaReadMultipleRecordsJSON

Java – read a JSON file – Multiple Records

Like, Share, And Subscribe | Professor Saad Yousuf
Watch Our All Videos On This :
Subscribe Our Channel :
Playlists:

JSON Jar File Link:
*******************CODE******************
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ReadJSONExample {

public static void main(String[] args) {
JSONParser jsonP = new JSONParser();

try(FileReader reader = new FileReader(“emps.json”)){
//Read JSON File
Object obj = jsonP.parse(reader);
JSONArray empList = (JSONArray) obj;
System.out.println(empList);
//Iterate over emp array
empList.forEach(emp -> parseEmpObj((JSONObject)emp));
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseEmpObj(JSONObject emp) {
JSONObject empObj = (JSONObject) emp.get(“employee”);
//get emp firstname, lastname, website
String fname = (String) empObj.get(“firstname”);
String lname = (String) empObj.get(“lastname”);
String website = (String) empObj.get(“website”);
System.out.println(“First Name: ” + fname);
System.out.println(“Last Name: ” + lname);
System.out.println(“Website: ” + website);
}
}

Java JSON read, Java JSON read multiple records, Java read JSON file, java programming, json, java tutorial, json schema, java class, json example, java code, java swing, java developer, java language, java json, java programming language, json data, learn java online, java main class, json format example, java course, object oriented programming, json schema example, javs software, learn java programming, java online course, java training, java web development

Xem Thêm Bài Viết Về Mobile Khác: https://sealsystem.org/mobile

Nguồn: https://sealsystem.org/

6 COMMENTS

  1. Hey Salam. How do we proceed if the objects arent seperated with "employee" tag, as in it the list contains different employee objects but they arent segregated under employee term or any other term for that matter.

  2. Hello. You tutorial on dom parser for reading XML was very informative. Please also do a tutorial on creating an XML(with nested nodes)using another XML as input with dom parser in Java. Thanks in advance.

LEAVE A REPLY

Please enter your comment!
Please enter your name here