Android Studio volley nested jsonarray

i am trying to get the title value of a json string (once it starts as an object and once as an array)

I managed to get a result, but it gives me the extra brackets and such.

title: {"value":"New Beginnings"}

instead of just

title: New Beginnings

The 2 urls i am trying this with are

https://jhookcrochet.eu/categories/yarniversity/rest?_format=json

https://jhookcrochet.eu/?_format=json

Any help on this will be highly appreciated! The code i came up with is:

RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());

String url = "https://jhookcrochet.eu/?_format=json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        String json = "";
        //txt.setText(response.toString());
        try {
            new JSONObject(response);
            json = "object";
        } catch (JSONException ex) {
            json = "array";
        }
        if (json == "array") {
            try {
                JSONArray jsonArray = new JSONArray(response);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jresponse =
                            jsonArray.getJSONObject(i);
                    String title = jresponse.getString("title");
                    //String field_image = jresponse.getString("field_image");
                    String body = jresponse.getString("body");
                    Log.d("title", title);
                    //Log.d("field_image",field_image);
                    Log.d("body", body);
                    textView.append("Title: " + title + "n");
                }
            } catch (JSONException e) {
                textView.append("NOn");
                e.printStackTrace();
            }
        }
        else if (json == "object") {
            textView.append("Yep, that is an objectn");
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray gettitle = jsonResponse.optJSONArray("title");
                for (int j=0;j<gettitle.length(); j++)
                {
                    String title = gettitle.getString(j);
                    Log.d("title", title);
                }
                //textView.append("Title: " + title + "n");
                //Log.d("title", title);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("error",error.toString());
    }
});
rq.add(request);

// Inflate the layout for this fragment
return root;

Answer

I actually just managed on how I want this to show.

                JSONArray jsonArray = jsonResponse.getJSONArray("title");
                JSONObject titlevalue = jsonArray.getJSONObject(0);
                String title = titlevalue.getString("value");

This will give the result I want.

Title: New Beginnings

In case someone is on the look for this as well, this is my snipped. Might be a nicer and better way, but this currently works for me 🙂

        else if (json == "object") {
            //textView.append("Yep, that is an objectn");
            try {
                JSONObject jsonResponse = new JSONObject(response);
                JSONArray jsonArray = jsonResponse.getJSONArray("title");
                JSONObject titlevalue = jsonArray.getJSONObject(0);
                String title = titlevalue.getString("value");
                JSONArray jsonBody = jsonResponse.getJSONArray("body");
                JSONObject bodyvalue = jsonBody.getJSONObject(0);
                String body = bodyvalue.getString("value");
                Log.d("title", title);
                Log.d("body", body);
                TitleView.append(title);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    textView.setText(Html.fromHtml(body, Html.FROM_HTML_MODE_COMPACT));
                } else {
                    textView.setText(Html.fromHtml(body));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }