-1

I am trying to get the data using async task. Results get properly in the jsonaray when i print the dta in the log. Iam trying to get the data from the json array but there is an eror received in the exception .is as followos I tried all solutions to convert fron jsonArray to obje and JsonObject to JsonArray.But It shows the following exception in the is as below.I dont know what should I have to do.Please suggest me the solution if any.I tried this functionality from 2 days but not solve.My code is as below.Error log is also at the bottom.
I am return that data from the api as follows

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

                    }

------------ Error Log --------------------

                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":"[email protected]","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
5
  • Possible duplicate of org.json.JSONArray cannot be converted to JSONObject Commented Sep 9, 2017 at 10:18
  • I already tried this at profile of type org.json.JSONObject cannot be converted to JSONArray and if convert then at profile of type JSONArray cannot be converted to JSONObject like errors are arises Commented Sep 9, 2017 at 10:38
  • Please see my error log block.is the array data is in right or format Commented Sep 9, 2017 at 10:40
  • I sent it for the php like echo get_xml(array('profile'=>$client),'client_profile'); this format Commented Sep 9, 2017 at 10:41
  • Please tell me any solution........! Commented Sep 9, 2017 at 12:03

1 Answer 1

0

Your JSON looks like this:

[
  {
    "profile": {
      "office_name": "",
      ...
      "office_email": ""
    }
  }
]

It is a JSON array with only a single element. To get that single element, you will first have to parse the JSON as an array and then get the first object from it.

Replace this line:

JSONObject ticket_obj = new JSONObject(result);

With these:

JSONArray ticket_arr = new JSONArray(result);
JSONObject ticket_obj = ticket_arr.getJSONObject(0);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.