位于org.json.JSON.typeMismatch的org.json.JSONException

时间:2015-08-08 18:00:35

标签: android json exception

我试图解析下面的json.However我没有得到我想要的结果。 我不明白为什么不匹配。

  [
  {
    "first_aired": "2014-07-14T01:00:00.000Z",
    "episode": {
      "season": 7,
      "number": 4,
      "title": "Death is Not the End",
      "ids": {
        "trakt": 443,
        "tvdb": 4851180,
        "imdb": "tt3500614",
        "tmdb": 988123,
        "tvrage": null
      }
    },
    "show": {
      "title": "True Blood",
      "year": 2008,
      "ids": {
        "trakt": 5,
        "slug": "true-blood",
        "tvdb": 82283,
        "imdb": "tt0844441",
        "tmdb": 10545,
        "tvrage": 12662
      }
    }
  },

这是我的java代码:

 package swipe.vivek.com.hollywood.Fragments;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import swipe.vivek.com.hollywood.R;
import swipe.vivek.com.hollywood.app.AppController;
import swipe.vivek.com.hollywood.helper.PopularInfo;
import swipe.vivek.com.hollywood.helper.PopularInfoAdapter;

/**
 * Created by Shiva on 08-08-2015.
 */
public class Calendar extends Fragment {

    private static final String TAG = Upcoming.class.getSimpleName();

    // Movies json url
    private static final String url = "https://private-780af2-trakt.apiary-mock.com/calendars/my/shows/2014-09-01/7";
    private ProgressDialog pDialog;
    private List<PopularInfo> bottom = new ArrayList<PopularInfo>();

    PopularInfoAdapter adapter;
    RecyclerView recyclerView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_main, container, false);
      /*  toolbar = (Toolbar)v.findViewById(R.id.toolbar);
        AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity();
        appCompatActivity.setSupportActionBar(toolbar); */
      /*  tabLayout = (TabLayout)v.findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText("page1"));
        tabLayout.addTab(tabLayout.newTab().setText("page2"));
        tabLayout.addTab(tabLayout.newTab().setText("page3"));
        tabLayout.addTab(tabLayout.newTab().setText("page3"));*/
       /* toolbar.setTitle("Upcomig movies"); */
        recyclerView = (RecyclerView) v.findViewById(R.id.cardList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new PopularInfoAdapter(getActivity(), bottom);
        recyclerView.setAdapter(adapter);
        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {


                Log.d(TAG, response.toString());
                hidePDialog();




                    try {
                        JSONObject jsonObject = response.getJSONObject(2);
                        JSONArray jsonArray = jsonObject.getJSONArray("show");

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



                          JSONObject jsonObjects = jsonArray.getJSONObject(j);
                            PopularInfo trailer = new PopularInfo();
                            trailer.setTitle(jsonObjects.getString("title"));
                            bottom.add(trailer);
                            adapter.notifyDataSetChanged();
                        }





                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                adapter.notifyDataSetChanged();

            }





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

        {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");
                headers.put("Authorization", "");
                headers.put("trakt-api-version", "2");
                headers.put("trakt-api-key", "");
                return headers;
            }
        };

        AppController.getInstance().addToRequestQueue(jsonArrayRequest);

        return v;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }
}

但我没有显示标题。一无所获。

1 个答案:

答案 0 :(得分:1)

讨论答案:

show中作为JSONArray访问JSONArray jsonArray = jsonObject.getJSONArray("show");对象将导致typeMismatch,因为它是一个对象。

要通过响应对象循环并获取标题,我们可以使用

for (int i = 0; i < response.length(); i++){
    JSONObject wholeObject = response.getJSONObject(i);
    JSONObject showObject = wholeObject.getJSONObject("show");
    String title = showObject.getString("title");
    Log.d("",title);
}

对于在谷歌这里磕磕绊绊的人:在JSON中区分objectsarrays的基本规则:对象在 {} 中关闭,数组在 [] (我们通常使用循环来获取数组)