Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

Hi I am trying to display the error messages stored in the following JSONObject obj

{"errors":["nickname is already taken,","email is already taken"]}

This is my implementation:

TextView errorMsg;
errorMsg = (TextView)findViewById(R.id.register_error);
String[] errorArray = (String[])obj.get("errors");                                       
errorMsg.setText(errorArray[0]);                                                         
Toast.makeText(getApplicationContext(), errorArray[0], Toast.LENGTH_LONG).show();        

However when trying to run the code, I get a ClassCastException

enter image description here

Can anyone explain to me the issue and how I can resolve it?

Thanks!

share|improve this question
up vote 1 down vote accepted

The "errors" array in your example isn't a String array (String[]), it's a JSONArray.

Instead, do

JSONArray errorArray = obj.getJSONArray("errors");

then,

errorMsg.setText(errorArray.getString(0));                                                         
Toast.makeText(getApplicationContext(), errorArray.getString(0), Toast.LENGTH_LONG).show();        
share|improve this answer

The 'erros' array is not String array. So get value in JSONArray do this.

JSONArray errorArray = obj.getJSONArray("errors");

you can convert it.

List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
    list.add(arr.getJSONObject(i).getString("name"));
}
share|improve this answer

Here it's an JsonArray and not an String array so use library like simple-json from google and parse using below code:

       JSONArray erroeArray= (JSONArray) jsonObject.get("errors");
        Iterator<String> iterator = erroeArray.iterator();
          while (iterator.hasNext()) {
            //yourcode here
          }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.