添加文本我將收到包含這一個JSON響應:根據JSON響應
{..."Weekday":[3,4,2],...} //numbers might not be in Ascending order
然後根據什麼「平日」裏我會打印出週日0週一1等。所以上面打印的文字是;
"Tuesday,Wednesday,Thursday" //This should be in ascending order
如何檢查和打印我想要什麼,我這樣做的方式是這樣的:
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");
我不知道用怎樣做到這一點,我想到的也許while循環或if語句會有幫助嗎?
--- 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]]
);
}
** ** 1排序的INT使用'Arrays.sort'數組。 ** 2 **迭代值** 3 **使用'DateFormatSymbols.getWeekdays'將int轉換爲String。也許這樣? – AxelH
在對數據進行排序之前,您已經忘記了將值放入您的「int []數字」中。你需要循環一次「數組」來初始化「數字」然後對其進行排序。然後你可以得到字符串值(把它們放在你想要的位置) – AxelH