我有一个JSON响应,但我坚持使用它

时间:2019-09-27 07:16:04

标签: java android json

我的json是:

{"status":"ok","result": 
{"provider":"instagram","title":"lovely_songs_14u","link": 
[{"link_name":"Download 
Video","video_link":"https:\/\/scontent.cdninstagram.com\/v\/t50.2886- 
16\/71650124_386743795596239_6119869649307488549_n.mp4?_nc_ht=scontent.cdninstagram.com&_nc_cat=111&oe=5D907D5B&oh=36ad5ad325cce7926307dfb730583b70"}]}}

Android Side:

StringRequest stringRequest = new StringRequest(Request.Method.GET, hp,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //hiding the progressbar after completion
                        Log.e("Response", response);

                        try {
                            JSONObject obj = new JSONObject(response);
                            categoryGetSet temp;

                            JSONArray ja_video = obj.getJSONArray("link");
                            for (int i = 0; i < ja_video.length(); i++) {
                                temp = new categoryGetSet();
                                JSONObject jo_data = ja_video.getJSONObject(i);
                                String txt_title = jo_data.getString("title");
                                String txt_link_name = jo_data.getString("link_name");
                                String txt_image = jo_data.getString("img_link");
                                String txt_video = jo_data.getString("video_link");

                                temp.setTitle(txt_title);
                                temp.setLinkName(txt_link_name);
                                temp.setImage(txt_image);
                                temp.setVideo(txt_video);

                                categoryGetSets.add(temp);

                                Log.e("dwnld", txt_link_name);
                            }

                            Log.e("try", "try");

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.e("catch", "catch");

                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        NetworkResponse networkResponse = error.networkResponse;
                        if (networkResponse != null) {
                            Log.e("Status code", String.valueOf(networkResponse.statusCode));
                            Toast.makeText(getActivity().getApplicationContext(), String.valueOf(networkResponse.statusCode), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        requestQueue.add(stringRequest);

但是每次都遇到问题时,请让我知道我在哪里做错了

3 个答案:

答案 0 :(得分:1)

因为链接数组位于结果对象内部,但您尝试从外部对象获取。因此,您将获得异常。

尝试这样解析

JSONObject obj = new JSONObject(response);
JSONObject resultObj=obj.getJSONObject("result");
JSONArray ja_video = resultObj.getJSONArray("link");

答案 1 :(得分:1)

您的JSON有效。

使用以下代码来解析您的JSON。

try {
    JSONObject obj = new JSONObject(json);
    categoryGetSet temp;

    JSONObject resultObject = (JSONObject) obj.get("result");

    String txt_title = resultObject.getString("title");
    String txt_provider = resultObject.getString("provider");
    JSONArray ja_video = resultObject.getJSONArray("link");

    for (int i = 0; i < ja_video.length(); ++i) {
        temp = new categoryGetSet();
        JSONObject jo_data = ja_video.getJSONObject(i);
        String txt_link_name = jo_data.getString("link_name");
        String txt_video = jo_data.getString("video_link");

        temp.setTitle(txt_title);
        temp.setLinkName(txt_link_name);
        temp.setImage(txt_provider);
        temp.setVideo(txt_video);

        Log.e("jsonResponse: ", "txt_title" + txt_title);
        Log.e("jsonResponse: ", "txt_link_name" + txt_link_name);
        Log.e("jsonResponse: ", "txt_provider" + txt_provider);
        Log.e("jsonResponse: ", "txt_video" + txt_video);
    }
} catch (JSONException e) {
    e.printStackTrace();
    Log.e("catch", e.getLocalizedMessage());
}

答案 2 :(得分:1)

因为您试图以错误的方式进行解析,所以它很流行,
您需要按顺序传递每个JSON对象和数组序列,贝娄是您的结构

  
      
  • 主要Json对象      
        
    • 结果对象      
          
      • 链接数组      
            
        • 带有数据的子对象
        •   
      •   
    •   
  •   

另一件事是您正在尝试一些不存在的密钥

尝试贝娄代码,它将为您提供帮助

StringRequest stringRequest = new StringRequest(Request.Method.GET, hp,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //hiding the progressbar after completion
                        Log.e("Response", response);
                        try {
                            JSONObject mainObject = new JSONObject(response);
                            JSONObject resultObj=mainObject.getJSONObject("result");
                            JSONArray linkJsonArrya = resultObj.getJSONArray("link");
                            categoryGetSet temp;

                            for (int i = 0; i < linkJsonArrya.length(); i++) {
                                temp = new categoryGetSet();
                                JSONObject jo_data = linkJsonArrya.getJSONObject(i);

                                if(jo_data.has("title")){
                                    temp.setTitle(jo_data.getString("title"));    
                                }else {
                                    temp.setTitle(jo_data.getString(""));
                                }

                                if(jo_data.has("link_name")){
                                    temp.setLinkName(jo_data.getString("link_name"));
                                }else {
                                    temp.setLinkName(jo_data.getString(""));
                                }

                                if(jo_data.has("img_link")){
                                    temp.setImage(jo_data.getString("img_link"));
                                }else {
                                    temp.setImage(jo_data.getString(""));
                                }

                                if(jo_data.has("video_link")){
                                    temp.setVideo(jo_data.getString("video_link"));
                                }else {
                                    temp.setVideo(jo_data.getString(""));
                                }
                                categoryGetSets.add(temp);
                            }
                            Log.e("try", "try");

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.e("catch", "catch");

                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        NetworkResponse networkResponse = error.networkResponse;
                        if (networkResponse != null) {
                            Log.e("Status code", String.valueOf(networkResponse.statusCode));
                            Toast.makeText(getActivity().getApplicationContext(), String.valueOf(networkResponse.statusCode), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        requestQueue.add(stringRequest);
    }