-1
我想解析下面的json.However但我沒有得到我想要的結果。 我不明白爲什麼有不匹配。org.json.JSONException在at org.json.JSON.typeMismatch
[
{
"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;
}
}
}
但我沒有得到顯示的標題。無所事事。從討論
您發佈的內容不是有效的JSON。給我們/將我們鏈接到您正在處理的整個文件版本。 – poss
這裏:http://pastebin.com/84iitz3v –
文件看起來像一個JsonArray,所以首先你應該將字符串提取到JSonArray,然後使用JSonObject作爲「顯示」對象。 – itwasntme