如何获取不带变量的JSONArray?

时间:2018-07-20 11:15:38

标签: android json retrofit

很抱歉问这样一个简单的问题,我很努力地在JSONArray中获取JSONArray中的值,而数组中没有变量。我在android studio中使用Retrofit库。给我链接或提示以获取价值,请帮助。提前TQ。

这是JSON格式:

 {  
   "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
         ],
         [  ],
         [  ],
         [  ],
         [  ],
         [  ],
         [  ]
      ]
   }
}

或可以查看json的链接:json link here

这是我的代码:

  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) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

尝试这种方式

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

**编辑**

    try {
        String st= (String) jsonTime.get(0);
        mTextview.setText(st);  //set the first item in array to textview
    } catch (JSONException e) {
        e.printStackTrace();
    }