2

I will receive a JSON respond containing this:

 {..."Weekday":[3,4,2],...} //numbers might not be in Ascending order

Then depending on what inside "Weekday" I will print out Sunday for 0, Monday for 1 and so on. So the text printed out for above would be;

"Tuesday,Wednesday,Thursday" //This should be in ascending order

How do i check and print what I want, the way I am doing this is like this:

JSONObject obj = new JSONObject(result); //result from backend
JSONObject DayOff = obj.getJSONObject("Weekday");

...

tvWeekdays = (TextView) getActivity().findViewById(R.id.tvWeekdays);
tvWeekdays.setText("The final result");

I am not sure with how to achieve this, I am thinking of maybe a while loop or if statement would help?

---UPDATE---

            JSONArray array = obj.getJSONArray("Weekday");
        if (array == null){

            tvDayOff = (TextView) getActivity().findViewById(R.id.tvDayOff);
            tvDayOff.setText("None");
        }
        // Create an int array to accomodate the numbers.
        int[] numbers = new int[array.length()];
        Arrays.sort(numbers);
        // Extract numbers from JSON array.
        for (int i = 0; i < array.length(); ++i) {
            numbers[i] = array.optInt(i);
            System.out.println(
                    DateFormatSymbols.getInstance().getWeekdays()[numbers[i]]
            );

        }
JerryKo
  • 384
  • 7
  • 25
  • 1
    **1** Sort the array of int using `Arrays.sort`. **2** iterate the values **3** Convert the int into String using `DateFormatSymbols.getWeekdays`. Maybe like this? – AxelH Dec 27 '16 at 07:53
  • You forgot to put values into your `int[] numbers` before sorting it. You need to loop once one the `array` to init `numbers` then sort it. Then you can get the String values (an put them where you want) – AxelH Dec 27 '16 at 09:27

2 Answers2

1

Here is a simple example of how to get the values depending on the locale :

public static void main(String[] args){
    for(int i = 0; i < 7; ++i)
        System.out.println(
            DateFormatSymbols.getInstance().getWeekdays()[i+1] //First cell is empty...
        );
}

This is my output (in French Locale) :

dimanche //Sunday
lundi //Monday
mardi
mercredi
jeudi
vendredi
samedi //Saterday

From this, you just need to order your array of integer using Arrays.sort(int[]) and get the Strings.

Without using the JSON, this would be done like this :

public static void main(String[] args){
    int[] array = {3, 5, 2}; //The array
    Arrays.sort(array);       //Sort the array
    for(int i = 0; i < array.length; ++i) //Loop on the result
    System.out.println(
        DateFormatSymbols.getInstance().getWeekdays()[array[i]]
    );
}

The last step would be to get the array, for this, I suggest you to read this answer

EDIT :

You can't do it in one loop. You first need to get the values then sort the array. Once this is done, you can get the String in the correct order

// Create an int array to accomodate the numbers.
int[] numbers = new int[array.length()];
    // Extract numbers from JSON array.
for (int i = 0; i < array.length(); ++i) {
        numbers[i] = array.optInt(i);
}
Arrays.sort(numbers);
for (int i = 0; i < numbers.length; ++i) {
     System.out.println(
         DateFormatSymbols.getInstance().getWeekdays()[numbers[i]]
     );
}
Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • Thanks for your answer, it is almost working. However there are still some problems, I've updated my answer but the weekdays showed are offset by 1. 1 for Sunday , 2 for Monday and so on. Also I think my array aren't sorted correctly. – JerryKo Dec 27 '16 at 09:15
  • @JerryKo see the edit. For the offset, the first day is Sunday but at index 1. You might need to use a modulo if you want the week to start on Monday. – AxelH Dec 27 '16 at 09:30
1
    JSONObject obj = new JSONObject(result); //result from backend
    JSONArray DayOff = obj.getJSONArray("Weekday"); // I think weekday is an array
// if weekday look like {"weekday":[2,4,3]}, its an array if so
String[] dayText = new String[DayOff.length()];
for(int i=0, len=DayOff.length(); i<len; i++){
switch (DayOff(i)) {
            case 0:  dayText[i] = "Sunday";
                     break;
            case 1:  dayText[i] = "Monday";
                     break;
            case 2:  dayText[i] = "Tuesday";
                     break;
}
}

try this..

Ciril
  • 420
  • 3
  • 9
  • This as some problem, first `DayOff(i)` is pseudocode, you need to use a getter to get a value. Second, you should order this array. And Last, A switch is not needed since those values exists – AxelH Dec 27 '16 at 08:16