0

I have a JSON:

{
    "Bills": {
        "name": "Mr.X",
        "creditCardNumber": "1234567890",
        "billDetails": [{
                "PaymentDueDate": "07-03-2018",
                "StatementDate": "24-02-2018",
                "AmountDue": "2156"
            },
            {
                "PaymentDueDate": "05-02-2018",
                "StatementDate": "23-01-2018",
                "AmountDue": "3256"
            },
            {
                "PaymentDueDate": "07-01-2018",
                "StatementDate": "24-12-2017",
                "AmountDue": "1132"
            },
            {
                "PaymentDueDate": "07-12-2017",
                "StatementDate": "24-10-2017",
                "AmountDue": "7654"
            }
        ]
    }
}

I am trying to read the JSON object but getting null for JSONArray i.e. billDetailsArr

 public static void readJSONFile() throws java.text.ParseException {
            JSONParser parser = new JSONParser();


            JSONObject data;
            try {
                data = (JSONObject) parser.parse(new FileReader("\\Bills.json"));
                JSONArray billdetailsArr = (JSONArray) data.get("billDetails");

                for(int i=0;i<data.size();i++)
            {

                JSONObject jsonobj_2 = (JSONObject) billdetailsArr.get(i);
                String dateStr = (String) jsonobj_2.get(new Date("StatementDate"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date StatementDate = sdf.parse(dateStr);
            }
}

My POJO class is:

public class BillDetails {

    private Date PaymentDueDate;
    private Date StatementDate;
    private String AmountDue;
}

I want to have StatementDate & PaymentDueDate in date format not as String. Is my code correct?

1
  • Post your error log. Commented Mar 14, 2018 at 11:39

1 Answer 1

1

The billDetails array isn't a property of the root element - it's an element within the bills element. So you need to get the bills element and then get the billsDetail array off that.

e.g.

data = (JSONObject) parser.parse(new FileReader("\\Bills.json"));
JSONObject billsObj = (JSONObject)data.get("bills");
JSONArray billdetailsArr = (JSONArray)billsObj.get("billDetails");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.