Solution 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.
Problem :
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();
Comments
Comment posted by minimal reproducible example
That’s not valid JSON. A JSON file cannot start with
Comment posted by mohamed salem
you are right, i have edited the question above , with part of the raw data , what do you think now ?
Comment posted by Stephen C
We still need a minimal reproducible example to understand what the type of
Comment posted by mohamed salem
response1 is JSONObject ,
Comment posted by mohamed salem
Great, so in your opinion , what’s the code to write to reach Fajr attribute taking into consideration the above JSON structure ?
Comment posted by mohamed salem
I added an image of the JSON file structure in the question header as well, what do you think now ?
Comment posted by Please do not upload images of code/data/errors when asking a question.
Please do not upload images of code/data/errors when asking a question.
Comment posted by mohamed salem
got the below error :
Comment posted by mohamed salem
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)