-1

Im sorry for asking such a simple question, im struggling to get the values in JSONArray in the JSONArray without the variables inside the Array. Im using Retrofit library in android studio.Give me link or hint to get the value, please help. TQ in advance.

And here is the JSON format:

 {  
   "meta":{  
      "code":200
   },
   "response":{  
      "provider":"jakim",
      "code":"wlp-0",
      "origin":"wlp-0",
      "jakim":"sgr03",
      "source":"http:\/\/www.e-solat.gov.my\/web\/muatturun.php?zone=sgr03&year=2018&bulan=7&jenis=year&lang=my&url=http:\/\/mpt.i906.my",
      "place":"Kuala Lumpur",
      "times":[  
         [  
            1531691280,
            1531696200,
            1531718520,
            1531730760,
            1531740600,
            1531745100
         ],
         [  ],
         [  ],
         [  ],
         [  ],
         [  ],
         [  ]
      ]
   }
}

or can view the link for json: json link here

Here is my code:

  private void getPrayTimeMalay(String code, String filter) {
    ApiServiceInterface apiEndPoint = Utility.getRetrofitInstanceMalay().create(ApiServiceInterface.class);
    Call<ResponseBody> call = apiEndPoint.getPrayerTimeMalay(code, filter);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                String tmp = response.body().string();
                JSONObject jsonData = new JSONObject(tmp);
                Log.d("getPrayer", "onResponse: " + jsonData);

                JSONObject jsonMeta = jsonData.getJSONObject("meta");
                JSONObject jsonResponse = jsonMeta.getJSONObject("response");

                if (jsonMeta.getInt("code") == 200) {
                    PrayerTime prayerTime = new PrayerTime();
                    prayerTime.code = jsonResponse.getString("code");
                    prayerTime.origin = jsonResponse.getString("origin");
                    prayerTime.place = jsonResponse.getString("place");

                    JSONArray jsonTime = new JSONArray("times");
                    for (int i = 0; i <= jsonTime.length(); i++) {


                    }

                }


            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Have you tried any methods on the that `jsonTime` variable? What errors do you get when you try? – OneCricketeer Jul 22 '18 at 04:34
  • Possible duplicate of [How do I parse JSON in Android?](https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android) – OneCricketeer Jul 22 '18 at 04:35
  • @cricket_007 I know how to parse json in android but this one a bit tricky. How to get the jsonArray and stored it, without the json variables. – Alhaadi Dev Jul 23 '18 at 02:45
  • after few hint from @Navneet Krishna i found the solution and want to share with the community. Here is the way on how to retrieve the data: check the edited code. – Alhaadi Dev Jul 23 '18 at 02:46
  • You don't have "json variables" in an array. You only have indices. The duplicate shows that you can loop over an array. Or you can directly call `get(index)`. You can post answers below rather than in the question. Besides that, you could just use Gson to map the JSON directly to a Java object – OneCricketeer Jul 23 '18 at 03:48

1 Answers1

0

try this way

JSONArray jsonTime = jsonResponse.getJSONArray("times");

** EDIT **

    try {
        String st= (String) jsonTime.get(0);
        mTextview.setText(st);  //set the first item in array to textview
    } catch (JSONException e) {
        e.printStackTrace();
    }
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
  • when i get the data in "times" and stored it. How can I retrieve the data and display one data in textview. – Alhaadi Dev Jul 21 '18 at 16:32
  • after few hint from @Navneet Krishna i found the solution and want to share with the community. Here is the way on how to retrieve the data: check the edited code. – Alhaadi Dev Jul 23 '18 at 02:35