jsonObject无法转换为json

jsonObject无法转换为json

杰森的问题。我得到一个响应,它显示在logcat中,但它说jsonObject无法转换为jsonArray。输入错过比赛。我是json方面的新手。所以请帮助我。

杰森的问题。我得到一个响应,它显示在logcat中,但它说jsonObject无法转换为jsonArray。输入错过比赛。我是json方面的新手。所以请帮助我。

 public class MainActivity extends Activity implements OnItemClickListener {
String pass;
String url = "http://pixel2share.com/quotesapp/webservices/getallcat.php/";

// JSON Node names
private static final String TAG_CATEGORIES = "response";
private static final String TAG_CATNAME = "catname";
private static final String TAG_CATID = "catid";
JSONArray categories = null;

ArrayList<String> catlist = new ArrayList<String>();

ListView list;

// String[] itemname ={
// "Good morning",
// "Good evening",
// "Funny",
// "Motivation"
// };

  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_list);

    // CustomListAdapter adapter=new CustomListAdapter(this, itemname);
    // list=(ListView)findViewById(R.id.listView1);
    // list.setAdapter(adapter);
    // list.setOnItemClickListener(this);

    new GetCategories().execute();

    System.out.println("11111111111111111111111111111111111");

   }

    @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

    // switch(position){
    //
    // case 0:
    // Intent intent=new Intent(getApplicationContext(),SecondScreen.class);
    // pass=String.valueOf(position);
    // intent.putExtra("pass", pass);
    // startActivity(intent);
    // overridePendingTransition
     (R.anim.slide_in_left,R.anim.slide_in_right);
    // break;
    // case 1:
    // Intent intent1=new Intent(getApplicationContext(),GoodEvening.class);
    // pass=String.valueOf(position);
    // intent1.putExtra("pass", pass);
    // startActivity(intent1);
    // overridePendingTransition(R.anim.left,R.anim.right);
    // Toast.makeText(getApplicationContext(), String.valueOf(position),
    // Toast.LENGTH_SHORT).show();
    // break;
    // case 2:
    // Intent intent2=new Intent(getApplicationContext(),Funny.class);
    // pass=String.valueOf(position);
    // intent2.putExtra("pass", pass);
    // startActivity(intent2);
    //     overridePendingTransition
         (R.anim.slide_out_left,R.anim.slide_out_right);
    // Toast.makeText(getApplicationContext(), String.valueOf(position),
    // Toast.LENGTH_SHORT).show();
    // break;
    // case 3:
    // Intent intent3=new Intent(getApplicationContext(),Motivation.class);
    // pass=String.valueOf(position);
    // intent3.putExtra("pass", pass);
    // startActivity(intent3);
    // overridePendingTransition(R.anim.flip_left_in,R.anim.flip_left_out);
    // Toast.makeText(getApplicationContext(), String.valueOf(position),
    // Toast.LENGTH_SHORT).show();
    // break;
    // }

}

// ///////////////////////////////////JSON
// CALL//////////////////////////////////////////////////

private class GetCategories extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        System.out.println("22222222222222222222222222222222222222222222");

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        ServiceHandler sh = new ServiceHandler();
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        System.out.println("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");

        Log.d("Response: ", "> " + jsonStr);
        System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");

        if (jsonStr != null) {
            System.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                System.out.println(jsonObj);

                // Getting JSON Array node
                categories = jsonObj.getJSONArray(TAG_CATEGORIES);

                     System.out.
                     println("33333333333333333333333333333333333333333");

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




                    // String id = c.getString(TAG_CATID);
                    String name = c.getString(TAG_CATNAME);
                    System.out
                            .println("5555555555555555555555555555555555");
                    System.out
                            .println("5555555555555555555555555555555555");
                    System.out
                            .println("5555555555555555555555555555555555");
                    // System.out.println(id);
                    System.out.println(name);
                    catlist.add(name);
                    // String email = c.getString(TAG_EMAIL);
                    // String address = c.getString(TAG_ADDRESS);
                    // String gender = c.getString(TAG_GENDER);

                    // // Phone node is JSON Object
                    // JSONObject phone = c.getJSONObject(TAG_PHONE);
                    // String mobile = phone.getString(TAG_PHONE_MOBILE);
                    // String home = phone.getString(TAG_PHONE_HOME);
                    // String office = phone.getString(TAG_PHONE_OFFICE);

                    // tmp hashmap for single contact
                    // String cat = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    // contact.put(TAG_ID, id);
                    // cat.put(TAG_CATNAME, name);
                    // contact.put(TAG_EMAIL, email);
                    // contact.put(TAG_PHONE_MOBILE, mobile);

                    // adding contact to contact list
                    // catlist.add(cat);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        // if (pDialog.isShowing())
        // pDialog.dismiss();
        // /**
        // * Updating parsed JSON data into ListView
        // * */
        // ListAdapter adapter = new SimpleAdapter(
        // MainActivity.this, contactList,
        // R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
        // TAG_PHONE_MOBILE }, new int[] { R.id.name,
        // R.id.email, R.id.mobile });

        // setListAdapter(adapter);
    }

}

}

最佳答案

我认为您的json就是这样,您可以获取如下数据:

JSONObject jsonObject = new JSONObject(responseString);

        JSONObject data_json = jsonObject.getJSONObject(“response”);

            status = data_json.getBoolean("Success");

          JSONArray itemInfo = data_json.getJSONArray("items");
           {
                      for (int i = 0; i < itemInfo.length(); i++) {
                        //Do your logic Here

                }
            }

09-30 11:05