Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – JSON File / Android – Get the value of string

Posted on November 11, 2022 By

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 ?

READ  [FIXED] android - recyclerview jetpack bug - pressing back button twice to go to previous fragment
Powered by Inline Related Posts

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)

Android Tags:android, arrays, java, json

Post navigation

Previous Post: [FIXED] Android ACTION_VIEW intent is not opening urls in non-browser apps like twitter and instagram
Next Post: [FIXED] android – Check if activity is active, and if active pass data from service to activity?

Related Posts

[FIXED] android – How to chain two text with ConstraintLayout Android
[FIXED] c# – CrossCurrentActivity is null in dependency even though it is initialized and not null in app Android
[FIXED] android – Multiple instances of same fragment type with different ViewModel type based on argument passed to fragment Android
[FIXED] java – I am using firebase UI login into my app but when i use Facebook builder it stops the app how to inflate that button..? Android
[FIXED] android – Kotlin sytax for ‘?’ constant issue Android
[FIXED] android – Is it possible to show Recyclerview from two lists of different types? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme