如何像Android中的响应一样设置数组对象的顺序?

时间:2016-07-27 07:27:10

标签: java android arrays json parsing

我正在使用volley类进行数组解析,我得到以下响应,但是,我想以相同的顺序显示数组项目,但是在解析后我的数组项目顺序不相同并显示随机顺序。

img

2 个答案:

答案 0 :(得分:1)

JsonObject jObject = new JsonObject(responseString);
JsonObject outputObject = jObject.get("output");
JsonObject golfObject = outputObject.get("GolfCourseTee");
ArrayList<GolfModel> golfList = new ArrayList<>();
JsonArray manArray = golfObject.getJsonArray("Men");
for(int i=0; i<manArray.length ;)
{
    String id = manArray.get(i).get("tee_id");
    String name = manArray.get(i).get("tee_name");
    String color = manArray.get(i).get("tee_color");
    GolfModel g = new GolfModel(id, name, color);
    golfList.add(g);
}
JsonArray ladiesArray = golfObject.getJsonArray("Ladies");
for(int i=0; i<ladiesArray.length ;)
{
    String id = ladiesArray.get(i).get("tee_id");
    String name = ladiesArray.get(i).get("tee_name");
    String color = ladiesArray.get(i).get("tee_color");
    GolfModel g = new GolfModel(id, name, color);
    golfList.add(g);
}
JsonArray juniorArray = golfObject.getJsonArray("Junior");
for(int i=0; i<juniorArray.length ;)
{
    String id = juniorArray.get(i).get("tee_id");
    String name = juniorArray.get(i).get("tee_name");
    String color = juniorArray.get(i).get("tee_color");
    GolfModel g = new GolfModel(id, name, color);
    golfList.add(g);
}

答案 1 :(得分:1)

Gson工作并维护JSON的顺序。

首先,我将JSON保存到类中并像这样使用它:

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.\"}}";
}

然后我将JSON映射到各自的模型:

    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;}
    }

这是我使用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();
            }
        }
    }

这是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

成员的附加日志:

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

正如您所看到的,订单与原始JSON中的相同。希望这会有所帮助。

编辑:更新的模型类GolfCourseTee返回一个也在TestActivity中使用的累积列表,也是添加的列表对应的日志。顺便说一句,这也可以通过其他方式解决,解决方案只展示了一种方法。