gpt4 book ai didi

java - jsonObject 无法转换,因为 jsonArray 类型不匹配

转载 作者:行者123 更新时间:2023-12-01 11:43:56 28 4
gpt4 key购买 nike

Json问题。我收到了一个响应,它显示在 logcat 中,但它说 jsonObject 无法转换为 jsonArray。类型未匹配。我是 json 方面的新手。所以请帮助我。

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);
}

}

}

最佳答案

试试这个方法

categories =(JSONArray)jsonObj.get("TAG_CATEGORIES");

而不是

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

关于java - jsonObject 无法转换,因为 jsonArray 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29314122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com