killer
killer

Reputation: 602

Value [{ of type java.lang.String cannot be converted to JSONObject in android

I have a json wcf service which I am trying to access on my localhost emulator. Service is accessed successfully but I am getting strange exception in my code. When I try to convert json string to json object I get the exception in logcat.

Value [{ of type java.lang.String cannot be converted to JSONObject 

Data I am getting from webservice is like this:

 "[{"message":"Valid user!","status":true}]"

My code is like this:

protected Boolean doInBackground(String... params) {

    String line = "";
    String ur = "http://10.0.2.2:28088/HighriseeSite/appservices.svc/login?username="+etUserName.getText().toString()+"&pass="+etPassword.getText().toString();
    Log.d("STRIMGuuuu",ur);

    try {
        // Replace it with your own WCF service path
        URL json = new URL(ur);
        URLConnection jc = json.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));

        line = reader.readLine();
        String jsonFormattedString = line.replaceAll("\\\\", "");
        Log.d("Json String--->",jsonFormattedString);

        JSONObject jsonmain = new JSONObject(jsonFormattedString);

        if(jsonmain.getBoolean("status")) {
            sharPref = getSharedPreferences("LoginInfo", MODE_PRIVATE);//mode private means that this logininfo cannot be accessed by other apps

            SharedPreferences.Editor editor = sharPref.edit();

            editor.putString("UserName", "sitemanager");
            editor.putString("Password", "admin123$");
            editor.putBoolean("Login", true);
            editor.commit();
        }

        return jsonmain.getBoolean("status");
    }
    catch(Exception e) {
        Log.d("Error--->",e.getMessage());
        return false;
    }
}

Logs from logcat are like this:

05-12 18:33:22.336    1874-1900/kanix.highrise.com.highrise D/Json String--->﹕ "[{"message":"Valid user!","status":true}]"
05-12 18:33:22.347    1874-1900/kanix.highrise.com.highrise D/Error--->﹕ Value [{ of type java.lang.String cannot be converted to JSONObject
05-12 18:33:22.354    1874-1895/kanix.highrise.com.highrise W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-12 18:33:22.354    1874-1895/kanix.highrise.com.highrise W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa591ee40, error=EGL_SUCCESS
05-12 18:33:22.383    1874-1886/kanix.highrise.com.highrise I/art﹕ Background sticky concurrent mark sweep GC freed 4572(191KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 1669KB/1884KB, paused 1.143ms total 139.646ms
05-12 18:33:22.706    1874-1895/kanix.highrise.com.highrise W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-12 18:33:22.706    1874-1895/kanix.highrise.com.highrise W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa591ee40, error=EGL_SUCCESS

Upvotes: 0

Views: 1392

Answers (3)

Exigente05
Exigente05

Reputation: 2211

"[{"message":"Valid user!","status":true}]"

If there exists double quotation at the beginning and end of the line then its not JSON data, it would a String that's why you are getting error. Correct JSON data would be [{"message":"Valid user!","status":true}] Then code like this,-

JSONArray array = new JSONArray(jsonString);
JSONObject object = array.getJSONObject(0);

Upvotes: 1

killer
killer

Reputation: 602

Got it! Needed to Remove character " on start and end of json string like this:

  jsonFormattedString= jsonFormattedString.substring(1, jsonFormattedString.length()-1) ;

My array now became

 [{"message":"Valid user!","status":true}]

Upvotes: 1

nafas
nafas

Reputation: 5423

you are casting JSONObject from a JSONArray.

JSONObject jsonmain = new JSONObject(jsonFormattedString); // jsonFormatteddString seems to be a JSONArray

try this :

JSONArray j = new JSONArray(jsonFormattedString);
JSONObject jsonmain = j.getJSONObject(0);

if you sometimes recieve JSONObject and sometimes JSONArray, you can try to change your code and check for what type you get.

A good example and explanation is at :

How to check whether the given object is object or Array in JSON string

Upvotes: 0

Related Questions