1

I need to show response on Sign Up, below is my JSON Response. I should show password is too short(minimum is 5 characters) into one string

 { errors: { password: [ "is too short (minimum is 5 characters)" ] } }

And also I need to parse the response from the following JSON data as Signature has already been taken

{ errors: { signature: [ "has already been taken" ] } }

Please tell me how to parse the particular data from the JSON data. Thanks in advance!!!!

Manjunath
  • 52
  • 1
  • 10

3 Answers3

1

You can use below method to parse your data.

 private String parseJsonData(String jsonResponse) {
        try {
            JSONObject jsonObject = new JSONObject(jsonResponse);
            JSONObject errorJsonObject = jsonObject.getJSONObject("errors");
            JSONArray jsonArray = null;
            //has method
            if (errorJsonObject.has("password")) {
                jsonArray = errorJsonObject.optJSONArray("password");
            } else if (errorJsonObject.has(" signature")) {
                jsonArray = errorJsonObject.optJSONArray("signature");
            }
            String errorMessage = jsonArray.getString(0);
            return errorMessage;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

You can replace unwanted symbols like below code:

errorMessage.repalce("[","");
errorMessage.repalce("]","");
errorMessage.repalce("/"","");
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • bro, thanks for your valuable time and reply. But I'm not getting output.. Im getting like this ["is too short (minimum is 5 characters)"] – Manjunath Feb 05 '19 at 07:00
  • I have run this code and i am getting text without [ ] . But you can remove it by replace method of String class. – Chetan Joshi Feb 05 '19 at 07:04
0

You can use Google's Gson library to do that using the following steps:

  1. Add dependency in your build.gradle(Module:app) file.

    dependencies { implementation 'com.google.code.gson:gson:2.8.5' }

    For latest version of gson library, click here

  2. To parse JSON string to an object use the code below:

    Gson gson = new Gson(); // I'm fetching my session stored JSON string // You can fetch as per your requirement String jsonStr = session.getJsonStr(); MyObject myObject = (MyObject) gson.fromJson(jsonStr, MyObject.class);

And if you need to convert an object to a JSON string, you can use the below code:

 // I'm fetching my session stored Object here
 // You can fetch as per your requirement
 MyObject myObject = session.getMyObject();
 String jsonStr = gson.toJson(myObject);

Make sure you design your object appropriate for the JSON string to match the data types. If you are not sure of the data types in the JSON, you can use this site or any parse and view website to view them.

Hope it helps!

vss
  • 1,093
  • 1
  • 20
  • 33
-1

Just try this,

          try {
                String tost = null;
                JSONObject object = new JSONObject(json);
                JSONObject errorObject = object.getJSONObject("errors");
                    if (errorObject.has("password")){
                        tost = "password "+errorObject.getJSONArray("password").get(0).toString();
                    } else if (errorObject.has("signature")){
                        tost = "signature "+errorObject.getJSONArray("signature").get(0).toString();
                    }
                    Toast.makeText(MainActivity.this, tost, Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    e.printStackTrace();
                }
  • thanks nanba, But I'm not getting the output as expected and Im getting null pointer exception for your code – Manjunath Feb 05 '19 at 07:02
  • if your response not contains "password" or "signature", you need to add one more else condition to set "toast = "your message";, then you may proceed . – Poovarasan Selvaraj Feb 05 '19 at 07:13