2013-05-09 124 views
2

我想从android应用程序中的服务器使用json数据。来自cakePHP web应用程序的数据格式如下所示;JSONArray无法转换为JSONObject Android

[{ “章”:{ “ID”: “1”, “章”: “4”, “CHAPTERTITLE”: “权利法案”}}]

和下面是Java代码正在用来实现我的目标;

public class Sheria extends SherlockListFragment { 

    // Progress Dialog 
// private ProgressDialog pDialog; 
// private View view; 

    // Creating JSON Parser object 
    JSONParser jParser = new JSONParser(); 

    ArrayList<HashMap<String, String>> chapterList; 

    // url to get all products list 
    // private static String url_all_products = 
    // "http://10.0.2.2/android_projects/sanisani/today_getall.php"; 
    private static String url_chapters = "http://10.0.2.2/constitution/laws/chapters"; 
    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_CHAPTERS = "Chapter"; 
    private static final String TAG_CHAP = "chapter"; 
    private static final String TAG_CHAP_TITLE = "chaptertitle"; 

    JSONArray chaps = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.sheria, container, false); 
     new FetchDetails().execute(); 

     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

    class FetchDetails extends AsyncTask<String, String, String> { 

     private ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(getSherlockActivity()); 
      pDialog.setMessage("Fetching Data..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
//   pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... arg0) { 
      // TODO Auto-generated method stub 
      chapterList = new ArrayList<HashMap<String, String>>(); 

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      // getting JSON string from URL 
      JSONObject json = jParser.getJSONFromUrl(url_chapters); 

      // Check your log cat for JSON reponse 
      Log.d("Chapters: ", json.toString()); 
      try { 
       // Checking for SUCCESS TAG 
//    int success = json.getInt(TAG_SUCCESS); 
//    if (success == 1) { 
        // products found 
        // Getting Array of Products 
        chaps = json.getJSONArray(TAG_CHAPTERS); 
        System.out.println(chaps); 

        // looping through All Products 
        for (int i = 0; i < chaps.length(); i++) { 
         JSONObject c = chaps.getJSONObject(i); 

         // Storing each json item in variable 
         String chapter = c.getString(TAG_CHAP); 
         String chapter_title = c.getString(TAG_CHAP_TITLE); 



         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_CHAP, chapter); 
         map.put(TAG_CHAP_TITLE, chapter_title); 

         // adding HashList to ArrayList 
         chapterList.add(map); 
         // getSherlockActivity().finish(); 
        } 

//    } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once product deleted 
      pDialog.dismiss(); 

      // Keys used in Hashmap 
      String[] from = { TAG_CHAP, TAG_CHAP_TITLE }; 

      // Ids of views in listview_layout 
      int[] to = { R.id.chapter, R.id.chaptertitle }; 

      // Instantiating an adapter to store each items 
      // R.layout.listview_layout defines the layout of each item 
      SimpleAdapter adapter = new SimpleAdapter(getActivity() 
        .getBaseContext(), chapterList, R.layout.sheriainfo, from, 
        to); 

      // Setting the adapter to the listView 
      setListAdapter(adapter); 

     } 

    } 

} 

当我运行代码时出现错误JSONArray无法转换为JSONObject。从getJSONFromUrl(String)

JSONArray json = jParser.getJSONFromUrl(url_chapters); 

returnJSONArray也删除chaps = json.getJSONArray(TAG_CHAPTERS);因为你TAG_CHAPTER不是数组:

请帮

回答

1

因为你得到一个JSONArray不是JSONObject,试试这个。

而在for循环做

JSONObject c = json.getJSONObject(i); 

代替

JSONObject c = chaps.getJSONObject(i); 

编辑

public class Sheria extends SherlockListFragment { 

// Progress Dialog 
// private ProgressDialog pDialog; 
// private View view; 

// Creating JSON Parser object 
JSONParser jParser = new JSONParser(); 

ArrayList<HashMap<String, String>> chapterList; 

// url to get all products list 
// private static String url_all_products = 
// "http://10.0.2.2/android_projects/sanisani/today_getall.php"; 
private static String url_chapters = "http://10.0.2.2/constitution/laws/chapters"; 
// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_CHAPTERS = "Chapter"; 
private static final String TAG_CHAP = "chapter"; 
private static final String TAG_CHAP_TITLE = "chaptertitle"; 

JSONArray chaps = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.sheria, container, false); 
    new FetchDetails().execute(); 

    return super.onCreateView(inflater, container, savedInstanceState); 
} 

class FetchDetails extends AsyncTask<String, String, String> { 

    private ProgressDialog pDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(getSherlockActivity()); 
     pDialog.setMessage("Fetching Data..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     // pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     chapterList = new ArrayList<HashMap<String, String>>(); 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     // getting JSON string from URL 

/* 这里的方法getJSONFromUrl()转换Stringü从服务器进入 JSONArrayJSONObject */

 JSONArray json = jParser.getJSONFromUrl(url_chapters); 

     // Check your log cat for JSON reponse 
     Log.d("Chapters: ", json.toString()); 
     try { 
      // Checking for SUCCESS TAG 
      // int success = json.getInt(TAG_SUCCESS); 
      // if (success == 1) { 
      // products found 
      // Getting Array of Products 
      // chaps = json.getJSONArray(TAG_CHAPTERS); 
      // System.out.println(chaps); 

      // looping through All Products 
      for (int i = 0; i < json.length(); i++) { 
       JSONObject c = json.getJSONObject(i); 
       JSONObject jObj = c.getJSONObject("TAG_CHAPTERS "); 

       // Storing each json item in variable 
       String chapter = jObj.getString(TAG_CHAP); 
       String chapter_title = jObj.getString(TAG_CHAP_TITLE); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(TAG_CHAP, chapter); 
       map.put(TAG_CHAP_TITLE, chapter_title); 

       // adding HashList to ArrayList 
       chapterList.add(map); 
       // getSherlockActivity().finish(); 
      } 

      // } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once product deleted 
     pDialog.dismiss(); 

     // Keys used in Hashmap 
     String[] from = { TAG_CHAP, TAG_CHAP_TITLE }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.chapter, R.id.chaptertitle }; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getActivity() 
       .getBaseContext(), chapterList, R.layout.sheriainfo, from, 
       to); 

     // Setting the adapter to the listView 
     setListAdapter(adapter); 

    } 

} 

} 
+0

我都试过,但它仍然没有工作。事实上,我得到了相反的结果,即JSONObject无法转换为JSONArray – 2013-05-10 08:20:45

+0

请参阅编辑。我已添加并更改您的代码。现在试试这个。 – 2013-05-10 08:58:32

相关问题