1

how to get each value of json array? im using append from textview to read all the value. but i want is how to get each value to set each textview.

public void JSONparse() {
    String url = "https://api.myjson.com/bins/mat2d";
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override public void onResponse(JSONObject response) {
            try {
                JSONArray JA = response.getJSONArray("products");
                for (int i=0; i<JA.length();i++){
                    Log.d("Result",JA.getString(i));
                    t1.append(JA.getString(i)+"\n");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    rq.add(request);
}

My Logcat

02-28 09:29:50.132 3101-3101/com.example.coorsdev.testing D/Result: Alfonso_Brandy 1000mL Imported Brandy
02-28 09:29:50.132 3101-3101/com.example.coorsdev.testing D/Result: Gran_Matador 350mL Regular Brandy
Mr.Kraken
  • 96
  • 1
  • 1
  • 11

1 Answers1

0

Use Stringutils.split() to split the string by whites paces. For example StringUtils.split("Hello World") returns "Hello" and "World"; or You can use the whitespace regex:

 String str = "Hello I'm your String";
    String[] splited = str.trim().split("\\s+"); //use trim to avoid empty strings

The result will be:

splited [0] == "Hello";
splited [1] == "I'm";
splited [2] == "Your";
splited [3] == "String";

  public void JSONparse() {
        String url = "https://api.myjson.com/bins/mat2d";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override public void onResponse(JSONObject response) {
                try {
                    JSONArray JA = response.getJSONArray("products");
                    for (int i=0; i<JA.length();i++){

                        StringUtils.split(JA.getString(i))

                             // or

                    String[] splited = JA.getString(i).trim().split("\\s+");

                        Log.d("Result",JA.getString(i));
                        t1.append(JA.getString(i)+"\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        rq.add(request);
    }
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47