Java – read JSON file – Multiple Records



#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);
}
}

Nguồn: https://baggersoriginals.com/

Xem thêm bài viết khác: https://baggersoriginals.com/cong-nghe/

Article Categories:
Công Nghệ

Comments

  • How can i access a json object inside a json object?

    likitha guntha July 1, 2020 1:58 pm Reply
  • Hello sir…i am getting error..Unexpected character () at position 0.

    Please help

    Pranta Pratim Roy July 1, 2020 1:58 pm Reply
  • 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.

    faisal jamal July 1, 2020 1:58 pm Reply

Leave a Reply

Your email address will not be published. Required fields are marked *