有没有办法从json网址获取数据

时间:2019-04-09 17:53:42

标签: android json android-volley

我正在使用android,并且尝试解析url中的json数据以从数组中获取字符串。

我尝试使用volley,但没有得到预期的结果。

{
code: 200,
status: "OK",
data: [
{
timings: {
Fajr: "04:13 (+04)",
Sunrise: "05:36 (+04)",
Dhuhr: "12:14 (+04)",
Asr: "15:42 (+04)",
Sunset: "18:51 (+04)",
Maghrib: "18:51 (+04)",
Isha: "20:15 (+04)",
Imsak: "04:03 (+04)",
Midnight: "00:14 (+04)"
},

StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        progressBar.setVisibility(View.GONE);

                        try {
                            //JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = new JSONArray(response);

                            for(int i=0; i<jsonArray.length(); i++){

                                JSONObject o = jsonArray.getJSONObject(i);
                                ModelActivity modelActivity = new ModelActivity(
                                        o.getString("Fajr")
                                        o.getString("Dhuhr"),
                                        o.getString("Asr"),
                                        o.getString("Maghrib"),
                                        o.getString("Isha")
                                );
                                modelActivityList.add(modelActivity);
                            }

我想要fajr dhuhr asr magrhrib isha个计时

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

如果您的请求返回了发布的JSON,那么问题不是不是执行请求的volley库,而是您尝试解析请求的方式。

在JSON表示中,{}表示您有一个对象。 还有[]有一个数组。

因此,您应该在第一个json对象中检查数据元素,然后在此数组中为每个对象检查时间,然后检查项目。

尽管我尚未编译或运行代码,但解析应该类似于以下内容:

JSONObject jsonObject = new JSONObject(response);
JSONArray data = jsonObject.getJSONObject("data");
for(int j=0; j<data.length(); j++){
    JSONObject jObj = data.getJSONObject(j);
    JSONArray jsonArrayTimings = new JSONArray(jObj.getJSONObject("timings"));

    for(int i=0; i<jsonArray.length(); i++){

        JSONObject o = jsonArray.getJSONObject(i);
        ModelActivity modelActivity = new ModelActivity(
                o.getString("Fajr")
                o.getString("Dhuhr"),
                o.getString("Asr"),
                o.getString("Maghrib"),
                o.getString("Isha")
        );
        modelActivityList.add(modelActivity);
    }
}

有关json解析的更多详细信息,您应该经历此link

答案 1 :(得分:0)

您应该使用JsonObjectRequest而不是StringRequest。这是一个示例:

    public void getRate(String tripDate) {

    Rate = 0.54;

    String url = "https://myserver/api/rates/" + tripDate;

    JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET,url,null,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {

                // since response is a JSONObject use getJSONArray to
                // get the JSONArray out of the JSONObject
                JSONArray rates = response.getJSONArray("rates");

            } catch (Exception e) {
                e.printStackTrace();
                Rate = 0.54;

                Log.i("Test", "Default Rate:" + Rate);

            }

        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Test", "Response Error: " + error.getLocalizedMessage());
            Rate = 0.54;

            Log.i("Test", "Default Rate:" + Rate);


        }
    }) {


   @Override
   public Map<String, String> getHeaders() throws AuthFailureError {
       HashMap<String, String> headers = new HashMap<String, String>();
        String authValue = "Bearer ";
        headers.put("Authorization", authValue);
        headers.put("Accept", "application/json; charset=utf-8");
        headers.put("Content-Type", "application/json; charset=utf-8");
       return headers;
   }


};


MyApplication.getInstance().addToRequestQueue(postRequest);

}

如果您的API返回JSONArray,则可以改用JSONArrayRequest ...

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
                    Request.Method.GET,
                    mJSONURLString,
                    null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {