-2

Hello I got json and i can echo displayname count and spam but i cant print results array.

["jack","sam","kelly"]

My json: {"results":[{"displayName":"Jack","count":"5","results":["jack","sam","kelly"],"spam":"14"}]}

My code:

 JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("results");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jo = jsonArray.getJSONObject(i);
                            name = jo.getString("displayName");
                            count = jo.getString("count");

                        }
                        name.setText(name);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user1318741
  • 21
  • 1
  • 6

1 Answers1

0

Please try below code it may solve your issue :

Java File :

public class MainActivity extends AppCompatActivity {


    TextView txtname,txtcount,txtspam,txtresults;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtname = (TextView)findViewById(R.id.txtname);
        txtcount = (TextView)findViewById(R.id.txtcount);
        txtspam = (TextView)findViewById(R.id.txtspam);
        txtresults = (TextView)findViewById(R.id.txtresults);


        String response = "{\"results\":[{\"displayName\":\"Jack\",\"count\":\"5\",\"results\":[\"jack\",\"sam\",\"kelly\"],\"spam\":\"14\"}]}";
        String name="",count="",spam="",results="";

        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(response);

            JSONArray jsonArray = jsonObject.getJSONArray("results");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = jsonArray.getJSONObject(i);
                name = jo.getString("displayName");
                count = jo.getString("count");
                spam = jo.getString("spam");
                JSONArray jsonArray1 = jo.getJSONArray("results");

                for(int j=0;j<jsonArray1.length();j++){
                    results += jsonArray1.get(j)+",";
                }
            }

            txtname.setText(name);
            txtcount.setText(count);
            txtspam.setText(spam);
            txtresults.setText(results);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

XML File :

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android" >

        <TextView
            android:id="@+id/txtname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/txtcount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtresults"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/txtspam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
Kaushal Gosaliya
  • 421
  • 7
  • 16