Home > OS >  How to parse JSON data in volley liabrary?
How to parse JSON data in volley liabrary?

Time:03-23

I want parse JSON response.I am unable to parse response.It shows org.json.JSONException: No value for String Response this error.

Here is my code ` public void onResponse(JSONObject response) {

                        Log.d("TAG", "Details:"   response);
                        responseTV.setText("String Response : "   response.toString());

                        try {
                            JSONObject jsonObject = response.getJSONObject("String Response" response);
                            strcode = jsonObject.getString("responseCode");
                            strtext = jsonObject.getString("responseText");
                            strname = jsonObject.getString("personName");
                            Log.i("TAG","parseData:" strname);
                            response_code.setText(""  strcode);
                            response_text.setText(""  strtext);
                            person_name.setText(""  strname);

                        } catch (JSONException e) {
                            Log.d("TAG", "profile: "   e);
                        }

                }`

CodePudding user response:

I guess you use Volley JsonObjectRequest, so you need to convert the response to a String and parse it to a JSONObject, like this:

jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                response -> {

                    try {

                        JSONObject jsonObject = new JSONObject(response.toString());
                        strcode = jsonObject.getString("responseCode");
                        strtext = jsonObject.getString("responseText");
                        strname = jsonObject.getString("personName");
                        Log.i("TAG","parseData:" strname);
                        response_code.setText(""  strcode);
                        response_text.setText(""  strtext);
                        person_name.setText(""  strname);

                    } catch (JSONException e) {
                        Log.d("TAG", "profile: "   e);
                    }


                }, error -> {}
        );

CodePudding user response:

 try {
                        JSONObject jsonObject = response.getJSONObject(response);
                        strcode = jsonObject.getString("responseCode");
                        strtext = jsonObject.getString("responseText");
                        strname = jsonObject.getString("personName");
                        Log.i("TAG","parseData:" strname);
                        response_code.setText(""  strcode);
                        response_text.setText(""  strtext);
                        person_name.setText(""  strname);

                    } catch (JSONException e) {
                        Log.d("TAG", "profile: "   e);
                    }

before you will do parsing , you will check all value receice by API exp :- responseCode, responseText, personName ( https://jsonlint.com/ ) , if all value you recived , check its in correct format , after that you will parse the data

you also use this , its will handle the JsonException

jsonObject.optString("responseText");

CodePudding user response:

Solved this problem with this code

public void onResponse(JSONObject response) {
    Log.d("TAG", "Details:"   response);
    responseTV.setText("String Response : "   response.toString());

    try {
        // JSONObject jsonObject = response.getJSONObject(response.toString());
        strcode = response.getString("responseCode");
        strtext = response.getString("responseText");
        strname = response.getString("personName");
        Log.i("TAGParser","parseData:" strname);
        response_code.setText(""  strcode);
        response_text.setText(""  strtext);
        person_name.setText(""  strname);
    } catch (JSONException e) {
        Log.d("TAG", "profile: "   e);
    }
}

CodePudding user response:

Use Gson library to convert string to json

  • Related