2016-07-24 34 views
0

我想在android應用程序中顯示json數據,但我有困難,我認爲可能與json文件的格式化方式有關.i想要獲取名稱和代碼的值在標題數組中。而且它不工作在android中顯示Json數據

這是JSON文件

{ 
status: "SUCCESS", 
headers: 
{ 
id: "4", 
name: "GLO", 
code: "GLO", 
background_color_code: "15B709", 
text_color_code: "ffffff", 
statusMessage: "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform" 
}, 
statusMessage: "Movies Loaded successfully", 
caption: "Discover" 
} 

這是javaclass

package com.example.cann; 

import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import org.json.JSONStringer; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 

import android.view.View; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 


public class CategoryActivity extends ListActivity { 

    private static final String TAG_POSTS = "headers"; 
    public static final String TAG_PIC = "name"; 
    public static final String TAG_NOTE = "code"; 
    JSONParser jParser; 
    private static final String URL_CATEGORY = "https://dobox.tv/api/home.json?platform=Android&api=1.1"; 
    private BaseAdapter mAdapter; 
    private ListView lv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_category); 
     lv = getListView(); 
     lv.setDivider(null); 

     lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { 

       Toast.makeText(CategoryActivity.this, "Item selected: " + position, 
         Toast.LENGTH_LONG).show(); 

       // Uncomment this to start a new Activity for a chosen item 
       /* Intent i = new Intent(getApplicationContext(), 
         ItemListActivity.class); 

       String category_id = ((TextView) view 
         .findViewById(R.id.category_id)).getText() 
         .toString(); 
       i.putExtra("category_id", category_id); 

       startActivity(i);*/ 
      } 
     }); 

        new LoadComments().execute(); 
    } 

    class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(CategoryActivity.this); 

      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 

     } 

     @Override 
     protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) { 

      ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>(); 
      jParser = new JSONParser(); 

      JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY); 

      try { 
        JSONArray categories =json.getJSONArray("headers"); 
       for (int i = 0; i < categories.length(); i++) { 
        String state = categories.getJSONObject(i).getString("name"); 
        String status = categories.getJSONObject(i).getString("code"); 

        HashMap<String, String> map = new HashMap<String, String>(); 
        map.put(TAG_PIC, state); 
        map.put(TAG_NOTE, status); 
        categoryList.add(map); 

       } 
      }catch (Throwable e){ 
       e.printStackTrace(); 
      } 
      return categoryList; 
     } 

     @Override 
     protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
      super.onPostExecute(result); 
      if(pDialog.isShowing()){ 
       pDialog.dismiss(); 
      } 
      mAdapter = new CategoryListAdapter(CategoryActivity.this,result); 
      lv.setAdapter(mAdapter); 
      mAdapter.notifyDataSetChanged(); 
     } 
    } 
} 

我收到此錯誤

07-24 12:20:50.849 16574-16591/com.example.cann W/System.err? org.json.JSONException: Value at headers of type java.lang.String cannot be converted to JSONArray 
07-24 12:20:50.852 16574-16591/com.example.cann W/System.err? at org.json.JSON.typeMismatch(JSON.java:100) 
07-24 12:20:50.852 16574-16591/com.example.cann W/System.err? at org.json.JSONObject.getJSONArray(JSONObject.java:588) 
07-24 12:20:50.853 16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:82) 
07-24 12:20:50.853 16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:61) 

回答

-1

您提供的JSON文件不是以有效的JSON格式。另外'headers'屬性包含一個JSONObject,而不是JSONArray。當您嘗試將JSONObject作爲JSONArray獲取時,您會得到JSON異常。

你可以試試這個:

{ 
    "status": "SUCCESS", 
    "headers": [ 
    { 
     "id": "4", 
     "name": "GLO", 
     "code": "GLO", 
     "background_color_code": "15B709", 
     "text_color_code": "ffffff", 
     "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform" 
    }, 
    { 
     "id": "5", 
     "name": "PLU", 
     "code": "PLU", 
     "background_color_code": "15B709", 
     "text_color_code": "ffffff", 
     "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform" 
    } 
    ], 
    "statusMessage": "Movies Loaded successfully", 
    "caption": "Discover" 
} 

請首先檢查您的JSON數據是否有效。您可以從herehere

+0

得到這個錯誤「W/System.err:org.json.JSONException:java.lang.String類型的標頭中的值不能轉換爲JSONObject」。當我試圖使用它作爲一個對象累...我知道如果我嘗試你粘貼它的格式將工作,我的問題是。是否有可能以我在我的問題中輸出它的方式顯示json文件 – arinze

+0

您可以按照我在此處粘貼的格式打印json響應。我的意思是所有的報價。 如果你想這樣做,只需做jsonResponse.toString(4); –