-2

My issue is that I don't how to find that I have read the first JSON object of the JSON array leave_applicants. This I need to find using java.

{
    "leave_applicants":[
        {
            "applicant_name" : "Jhon",
            "supervisor_name" : "Mark",
            "org" : "UNDP",
            "index_no" : 1,
            "leave_details": {
                "leave_type" : "annual",
                "from" : "12-07-2018",
                "to" : "15-07-2018"
            }
        },
        {
            "applicant_name" : "Ravi",
            "supervisor_name" : "Mark",
            "org" : "UNDP",
            "index_no" : 2,
            "leave_details": {
                "leave_type" : "sick_leave_cert",
                "from" : "20-07-2018",
                "to" : "25-07-2018"
            }
        }
    ]
}
Sudhanshu Vohra
  • 1,345
  • 2
  • 14
  • 21

1 Answers1

0

Assuming the JSON is in a text file stored on your computer. See the following code and at the end of each iteration of the for loop the end of first JSON object of the JSON array will occur (as you wanted).

import java.io.FileReader;
import java.util.Iterator;

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

public class ReadingJSONFromFile {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        JSONParser parser = new JSONParser();

        try {

            Object obj = parser.parse(new FileReader(
                    "Path/To/JSON/File/json_file.txt"));

            JSONObject jsonObject = (JSONObject) obj;
            JSONArray leaveApplicants = (JSONArray) jsonObject.get("leave_applicants");

            for(Object applicants: leaveApplicants) {
                JSONObject app = (JSONJObject)applicants;
                String name = (String) app.get("applicant_name"); // Get the applicant name
                // You can get the other keys of the object similarly and display them as you want
                // This will be the end of the first object of the JSON Array
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Sudhanshu Vohra
  • 1,345
  • 2
  • 14
  • 21
  • Thank you... Sir I have one more issue i.e. I want to print that in two different pdf file mean after I reach to( "to" : "15-07-2018") want to stop and print it in pdf file. So how will I stop at that position.Than after that I want to continue with the other applicant – rohit kumar Sep 04 '18 at 06:01
  • @rohitkumar write a function to print the contents of the above specified JSON format in PDF and at the end of each iteration of the for loop call that function to print a PDF file for each respective entry of the JSON Array. You can refer to this: https://blog.idrsolutions.com/2010/01/printing-pdf-files-from-java/ – Sudhanshu Vohra Sep 04 '18 at 06:18
  • From were can I download JSONParser package whatever I am downloading that is creating error for whole class – rohit kumar Sep 04 '18 at 09:27