Vishal Vishal - 4 months ago 6
JSON Question

how to set order of array object just like response in Android?

I am using volley class for array parsing , I am getting following response but , I want to display in same order of array items which is in response but after parsing my array items order are not same and display random order.

{
"output": {
"status": "1",
"GolfCourseTee": {
"Men": [
{
"tee_id": 2,
"tee_name": "Blue",
"tee_color": "#000099"
},
{
"tee_id": 4,
"tee_name": "Yellow",
"tee_color": "#FFFF00"
},
{
"tee_id": 5,
"tee_name": "White",
"tee_color": "#FFFFFF"
},
{
"tee_id": 1,
"tee_name": "Black",
"tee_color": "#000000"
},
{
"tee_id": 3,
"tee_name": "Red",
"tee_color": "#FF0000"
}
],
"Ladies": [
{
"tee_id": 2,
"tee_name": "Blue",
"tee_color": "#000099"
},
{
"tee_id": 4,
"tee_name": "Yellow",
"tee_color": "#FFFF00"
},
{
"tee_id": 5,
"tee_name": "White",
"tee_color": "#FFFFFF"
},
{
"tee_id": 1,
"tee_name": "Black",
"tee_color": "#000000"
},
{
"tee_id": 3,
"tee_name": "Red",
"tee_color": "#FF0000"
}
],
"Junior": [
{
"tee_id": 2,
"tee_name": "Blue",
"tee_color": "#000099"
},
{
"tee_id": 4,
"tee_name": "Yellow",
"tee_color": "#FFFF00"
},
{
"tee_id": 5,
"tee_name": "White",
"tee_color": "#FFFFFF"
},
{
"tee_id": 1,
"tee_name": "Black",
"tee_color": "#000000"
},
{
"tee_id": 3,
"tee_name": "Red",
"tee_color": "#FF0000"
}
]
},
"message": "Golf Course Tee List."
}
}

Answer

Gson works and maintains the order of JSON.

First, I saved the JSON into a class and used it like so:

public class TestJSON {

    public static String TEST_JSON_2 = "{\"output\":{\"status\":\"1\",\"GolfCourseTee\":{\"Men\":[{\"tee_id\":2,\"tee_name\":\"Blue\",\"tee_color\":\"#000099\"},{\"tee_id\":4,\"tee_name\":\"Yellow\",\"tee_color\":\"#FFFF00\"},{\"tee_id\":5,\"tee_name\":\"White\",\"tee_color\":\"#FFFFFF\"},{\"tee_id\":1,\"tee_name\":\"Black\",\"tee_color\":\"#000000\"},{\"tee_id\":3,\"tee_name\":\"Red\",\"tee_color\":\"#FF0000\"}],\"Ladies\":[{\"tee_id\":2,\"tee_name\":\"Blue\",\"tee_color\":\"#000099\"},{\"tee_id\":4,\"tee_name\":\"Yellow\",\"tee_color\":\"#FFFF00\"},{\"tee_id\":5,\"tee_name\":\"White\",\"tee_color\":\"#FFFFFF\"},{\"tee_id\":1,\"tee_name\":\"Black\",\"tee_color\":\"#000000\"},{\"tee_id\":3,\"tee_name\":\"Red\",\"tee_color\":\"#FF0000\"}],\"Junior\":[{\"tee_id\":2,\"tee_name\":\"Blue\",\"tee_color\":\"#000099\"},{\"tee_id\":4,\"tee_name\":\"Yellow\",\"tee_color\":\"#FFFF00\"},{\"tee_id\":5,\"tee_name\":\"White\",\"tee_color\":\"#FFFFFF\"},{\"tee_id\":1,\"tee_name\":\"Black\",\"tee_color\":\"#000000\"},{\"tee_id\":3,\"tee_name\":\"Red\",\"tee_color\":\"#FF0000\"}]},\"message\":\"Golf Course Tee List.\"}}";
}

Then I mapped the JSON to their respective models:

    public class Data {

        @SerializedName("output")
        Output output;

        public Output getOutput() {return output;}
    }

    public class Output {

        @SerializedName("status")
        String status;

        @SerializedName("GolfCourseTee")
        GolfCourseTee GolfCourseTee;

        @SerializedName("message")
        String message;

        public com.pearson.android.test.GolfCourseTee getGolfCourseTee() {return GolfCourseTee;}

        public String getMessage() {return message;}

        public String getStatus() {return status;}
    }

    public class GolfCourseTee {

        @SerializedName("Men")
        List<Member> Men;

        @SerializedName("Ladies")
        List<Member> Ladies;

        @SerializedName("Junior")
        List<Member> Junior;

        List<Member> cumulativeList;

        public List<Member> getAllMembers(){

            if(cumulativeList == null){  
                cumulativeList = new ArrayList<Member>();
                cumulativeList.addAll(Men);
                cumulativeList.addAll(Ladies);
                cumulativeList.addAll(Junior);
            }
            return cumulativeList;
        }

        public List<Member> getJunior() {return Junior;}

        public List<Member> getLadies() {return Ladies;}

        public List<Member> getMen() {return Men;}
    }

    public class Member {

        @SerializedName("tee_id")
        int tee_id;

        @SerializedName("tee_name")
        String tee_name;

        @SerializedName("tee_color")
        String tee_color;

        public int getTee_id() {return tee_id;}

        public String getTee_color() {return tee_color;}

        public String getTee_name() {return tee_name;}
    }

And here is the Activity where I use Gson:

    public class TestActivity extends AppCompatActivity {


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

            Gson gson = new Gson();

            try{

                Data data = gson.fromJson(TestJSON.TEST_JSON_2,Data.class);

                List<Member> mensList = data.getOutput().getGolfCourseTee().getMen();
                List<Member> ladiesList = data.getOutput().getGolfCourseTee().getLadies();
                List<Member> juniorsList = data.getOutput().getGolfCourseTee().getJunior();
                List<Member> cumulativeList = data.getOutput().getGolfCourseTee().getAllMembers();

                for(Member member : mensList)
                    Log.v("men","Id: " + member.getTee_id() + " name: " + member.getTee_name() + " color: " + member.getTee_color());

                for(Member member : ladiesList)
                    Log.v("ladies","Id: " + member.getTee_id() + " name: " + member.getTee_name() + " color: " + member.getTee_color());

                for(Member member : juniorsList)
                    Log.v("juniors","Id: " + member.getTee_id() + " name: " + member.getTee_name() + " color: " + member.getTee_color());

                for(Member member : cumulativeList){
                    Log.v("member","Id: " + member.getTee_id() + " name: " + member.getTee_name() + " color: " + member.getTee_color());
                }

            }catch(Exception exception){
                exception.printStackTrace();
            }
        }
    }

And here is Logcat:

07-27 19:53:42.204 16850-16850/com.android.test V/men: Id: 2 name:Blue color:#000099
07-27 19:53:42.204 16850-16850/com.android.test V/men: Id: 4 name:Yellow color:#FFFF00
07-27 19:53:42.204 16850-16850/com.android.test V/men: Id: 5 name:White color:#FFFFFF
07-27 19:53:42.204 16850-16850/com.android.test V/men: Id: 1 name:Black color:#000000
07-27 19:53:42.204 16850-16850/com.android.test V/men: Id: 3 name:Red color:#FF0000
07-27 19:53:42.204 16850-16850/com.android.test V/ladies: Id: 2 name:Blue color:#000099
07-27 19:53:42.204 16850-16850/com.android.test V/ladies: Id: 4 name:Yellow color:#FFFF00
07-27 19:53:42.204 16850-16850/com.android.test V/ladies: Id: 5 name:White color:#FFFFFF
07-27 19:53:42.204 16850-16850/com.android.test V/ladies: Id: 1 name:Black color:#000000
07-27 19:53:42.204 16850-16850/com.android.test V/ladies: Id: 3 name:Red color:#FF0000
07-27 19:53:42.205 16850-16850/com.android.test V/juniors: Id: 2 name:Blue color:#000099
07-27 19:53:42.205 16850-16850/com.android.test V/juniors: Id: 4 name:Yellow color:#FFFF00
07-27 19:53:42.205 16850-16850/com.android.test V/juniors: Id: 5 name:White color:#FFFFFF
07-27 19:53:42.205 16850-16850/com.android.test V/juniors: Id: 1 name:Black color:#000000
07-27 19:53:42.205 16850-16850/com.android.test V/juniors: Id: 3 name:Red color:#FF0000

And the additional logs for member:

07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 2 name: Blue color: #000099
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 4 name: Yellow color: #FFFF00
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 5 name: White color: #FFFFFF
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 1 name: Black color: #000000
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 3 name: Red color: #FF0000
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 2 name: Blue color: #000099
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 4 name: Yellow color: #FFFF00
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 5 name: White color: #FFFFFF
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 1 name: Black color: #000000
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 3 name: Red color: #FF0000
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 2 name: Blue color: #000099
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 4 name: Yellow color: #FFFF00
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 5 name: White color: #FFFFFF
07-28 13:53:10.780 6680-6680/com,android.test V/member: Id: 1 name: Black color: #000000
07-28 13:53:10.780 6680-6680/com.android.test V/member: Id: 3 name: Red color: #FF0000

As you can see the order is just like in the original JSON. Hope this helps.

Edit: updated model class GolfCourseTee to return a cumulative list that is also used in TestActivity, also logs corresponding to the list added. Btw this can also be solved in other ways, solution only demonstrates one approach.