pradeep72767
pradeep72767

Reputation: 21

I got exception while converting String to JSONObject

I am trying to consume restApi using volley. I am getting this Exception:

org.json.JSONException: Value {"Emp_name":"Abc1","DOJ":"01-01-2021","DOB":"10-01-1997","Manager_name":"abc","designation":"TL"} of type java.lang.String cannot be converted to JSONObject

I tried many solutions from Stack Overflow but still it's not solved.

MainActivity.java


private void getData() {
    mRequestQueue = Volley.newRequestQueue(MainActivity.this);
    String url = "https://indofinpay.com/REST_API/api/Demo/GetData?id=" + edtname.getText().toString();


    TrustManagerManipulator.allowAllSSL();
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {

                @SuppressLint("LongLogTag")
                @Override
                public void onResponse(String  response) {


                    Toast.makeText(getApplicationContext(), "Response :" + response, Toast.LENGTH_LONG).show();
                    try {
                             JSONObject jsonObject = new JSONObject(response);

                            String empNAme = jsonObject.getString("Emp_name");
                            String doj = jsonObject.getString("DOJ");
                            String dob = jsonObject.getString("DOB");
                            String Manager_name = jsonObject.getString("Manager_name");
                            String designation = jsonObject.getString("designation");
                         } catch (JSONException e) {
                          e.printStackTrace();
                      
                         }
                   }
                  }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "error :" + error.toString(), Toast.LENGTH_LONG).show();
        }
    });
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.RestAPI_Consume"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

Response in String:

"{\"Emp_name\":\"Abc1\",\"DOJ\":\"01-01-2021\",\"DOB\":\"10-01-1997\",\"Manager_name\":\"abc\",\"designation\":\"TL\"}"

I am trying to convert this response into JSONObject.

proper jsonobject where I can fetch value from its name.

Upvotes: 1

Views: 66

Answers (1)

Mr. Mad
Mr. Mad

Reputation: 1238

the response you shared is not a valid json string , just becasue of interted commas of start and end . Your string :

"{"Emp_name":"Abc1","DOJ":"01-01-2021","DOB":"10-01-1997","Manager_name":"abc","designation":"TL"}"

and valid Json will be as :

{"Emp_name":"Abc1","DOJ":"01-01-2021","DOB":"10-01-1997","Manager_name":"abc","designation":"TL"}

SO basically either do yourself or ask to backend developer to correct the response with json_encode($RESPONSE) NOTE : $RESPONSE is the result array in api.

Upvotes: 2

Related Questions