0

I have the following JSON file format :

{"code":200,
 "status":"OK",
 "data":[
   {"timings":
     {"Fajr":"03:11 (EET)",
      "Sunrise":"04:54 (EET)",
      "Dhuhr":"11:53 (EET)",
      "Asr":"15:29 (EET)",
      "Sunset":"18:52 (EET)",
      "Maghrib":"18:52 (EET)",
      "Isha":"20:23 (EET)",
      "Imsak":"03:01 (EET)",
      "Midnight":"23:53 (EET)"},....

I want to get the value of Asr (for example) & parse it into string variable, I tried the below but nothing works (Please note: response is successfully retrieved, so no issue with reaching the JSON file, but only to get the value of string).

String mfajr = response1.getJSONArray("data").getJSONObject(0)
                        .get("Fajr").toString();

String  mdoher = response1.getJSONArray("data").getJSONObject(0)
                          .getJSONArray("timings")
                          .get(Integer.parseInt("Dhuhr")).toString();
4
  • That's not valid JSON. A JSON file cannot start with HTTP/1.1 blah blah, and it cannot end with },. Please show us a minimal reproducible example of your attempt so that we can figure out what you are actually doing. Commented Jun 12, 2022 at 1:30
  • you are right, i have edited the question above , with part of the raw data , what do you think now ? Commented Jun 12, 2022 at 1:46
  • We still need a minimal reproducible example to understand what the type of response1 is. And what the exceptions (or whatever) are. Commented Jun 12, 2022 at 1:50
  • response1 is JSONObject , Commented Jun 12, 2022 at 11:47

1 Answer 1

1

Assuming that response1 is a JSONObject.

String mfajr = response1.getJSONArray("data")
                        .getJSONObject(0)
                        .get("Fajr").toString();

That will fail because getJSONObject(0) returns an object that has a "timings" attribute ... not a "Fajr" attribute.

String  mdoher = response1.getJSONArray("data")
                          .getJSONObject(0).getJSONArray("timings")
                          .get(Integer.parseInt("Dhuhr")).toString();

That will fail because the value of "timings" attribute is JSON object, not a JSON array.

Also, the value of the "Dhuhr" attribute is not an integer, so parseInt is not going to work. If you want the value as an integer, you are going to do some string analysis to convert "11:53 (EET)" to an integer.

Basically, you need to make sure that your Java code precisely matches the JSON structure. Close enough is NOT good enough.


OK .... so you want someone to write your code for you.

Try this:

String mfajr = response1.getJSONArray("data")
                        .getJSONObject(0)
                        .getJSONObject("timings")
                        .getString("Fajr");

Now, compare it with the structure of the JSON.

Sign up to request clarification or add additional context in comments.

7 Comments

Great, so in your opinion , what's the code to write to reach Fajr attribute taking into consideration the above JSON structure ?
I added an image of the JSON file structure in the question header as well, what do you think now ?
Please do not upload images of code/data/errors when asking a question.. In this case, it adds precisely zero ... because we already have the JSON as text.
got the below error :
2022-06-12 16:07:20.486 13041-13041/com.company.newsalah W/System.err: org.json.JSONException: Value {"Fajr":"03:11 (EET)","Sunrise":"04:54 (EET)","Dhuhr":"11:53 (EET)","Asr":"15:29 (EET)","Sunset":"18:52 (EET)","Maghrib":"18:52 (EET)","Isha":"20:23 (EET)","Imsak":"03:01 (EET)","Midnight":"23:53 (EET)"} at timings of type org.json.JSONObject cannot be converted to JSONArray 2022-06-12 16:07:20.486 13041-13041/com.company.newsalah W/System.err: at org.json.JSON.typeMismatch(JSON.java:101)
|

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.