我正在尝试使用异步任务获取数据。当我在日志中打印dta时,结果在jsonaray中正确显示。 Iam试图从json数组中获取数据但是在异常中收到了一个错误。作为followos我尝试了所有解决方案将fron jsonArray转换为obje和JsonObject转换为JsonArray.But它显示以下异常如下所示。我不知道该怎么办。如果有的话,请给我建议解决方案。我从2天开始试用这个功能但没解决。我的代码如下所示。错误日志也在底部。
我从api返回数据如下
echo get_xml(array('profile'=>$client),'client_profile');
------------ Code --------------------
private class client_details extends AsyncTask<Void, Void, JSONArray>
{
Dialog dialog;
@Override
public void onPreExecute()
{
dialog = new Dialog(Clients.this,android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progressbar);
dialog.show();
}
@Override
protected JSONArray doInBackground(Void... params) {
// TODO Auto-generated method stub
Shared_pref Prefs = new Shared_pref(getApplicationContext());
String table_pref=Prefs.getTabel_prefix();
String vis_client_id=Prefs.getClient_Id();
String filter_id="&vis_filter_id=1";
URL=op.getUrl(getApplicationContext(),"client","view_profile&vis_client_id="+vis_client_id,filter_id);
JSONArray client_array = JSONfunctions.getJSONfromURL(URL+"&vis_encode=json",Clients.this);
return client_array;
}
@Override
public void onPostExecute(JSONArray client_array)
{
super.onPostExecute(client_array);
String result =client_array.toString();
if(result.equals("[\"Nodatafound\"]"))
{
Operation.showToast(getApplicationContext(), R.string.Data_not_found);
}
try
{
//ticket_obj=client_array.getJSONObject(0);
JSONObject ticket_obj = new JSONObject(result);
//tickets_data_array=ticket_obj.getJSONArray("client_profile");
if (ticket_obj.has("profile")) {
op.showToast(getApplicationContext(), "Key found ........!");
JSONArray jsonarray = new JSONArray(client_array);//THIS ONE
JSONObject jsonobject = jsonarray.getJSONObject(0);
String Fname = jsonobject.getString(FIRST_NAME);
Log.d("Fname ===> ",Fname);
}
else
{
op.showToast(getApplicationContext(), "No such key found........!");
Log.d("No such key found........!","No such key found........!");
}
}
//
catch (JSONException e) { e.printStackTrace();}
}
dialog.dismiss();
}
------------错误日志--------------------
09-09 06:10:36.623: W/System.err(9002): org.json.JSONException: Value [{"profile":{"office_name":"","is_verified":"1","forum_email_notification":"1","city":"","first_name":"Pratik","balance":"0","option_domain_name":"test","initials":"Mr","client_type":"0","group_credit":"0.00","gender":"0","is_approved":"1","domain_name":"test","client_id":"2","otherim":"","credit":"0.00","is_spam":"0","office_phone":"","parent_id":"0","domain_url":"http:\/\/test.com\/test\/sandip\/v5\/","fax":"","forum_signature_content":"","group_id":"1","optionB_domain_id":"1","country":"","contact_number":"","user_name":"test","email":"pratik@test.com","job_title":"","last_name":"T","parent_name":"","temporary_address":"","facebook_id":"","language":"1","office_contact_num":"","salt":"testtestestest","zip_code":"","office_website":"","department":"","state":"","office_fax":"","login_as":"0","currency":"USD","permanent_address":"","department_access":"0","is_manager":"0","birthdate":"","registration_date":"1504254310","parent_email":"","password_auth":"0","group_name":"Default","netmeetingid":"","website":"","domain_id":"1","is_banned":"0","twitter_id":"","mobile_number":"","office_address":"","authentication":"383Y4A==","harvest_id":"0","image_path":"","office_email":""}}] of type org.json.JSONArray cannot be converted to JSONObject
答案 0 :(得分:0)
您的JSON如下所示:
[
{
"profile": {
"office_name": "",
...
"office_email": ""
}
}
]
它是一个只有一个元素的JSON数组。要获得该单个元素,首先必须将JSON解析为数组,然后从中获取第一个对象。
替换此行:
JSONObject ticket_obj = new JSONObject(result);
有了这些:
JSONArray ticket_arr = new JSONArray(result);
JSONObject ticket_obj = ticket_arr.getJSONObject(0);