[英]java servlet sort json data by month name
I have a json array having single object with key as month name. 我有一个具有键作为月名称的单个对象的json数组。 When coming from Db it looks like
[{ "January": "January", "February": "February", "March": "March", "April": "April", "May": "May", "June": "June", "July": "July", "August": "August", "September": "September", "October": "October", "November": "November", "December": "December" }]
and on frontend it looks like 当从Db传来时,它看起来像
[{ "January": "January", "February": "February", "March": "March", "April": "April", "May": "May", "June": "June", "July": "July", "August": "August", "September": "September", "October": "October", "November": "November", "December": "December" }]
,在前端看起来像
<select data-field-id="317" class="form-control selectchange to_month" id="searchform_to_month" name="searchform_to_month"><option value="">Select To Month</option><option value="June">June</option><option value="October">October</option><option value="December">December</option><option value="May">May</option><option value="September">September</option><option value="March">March</option><option value="July">July</option><option value="January">January</option><option value="February">February</option><option value="April">April</option><option value="August">August</option><option value="November">November</option></select>
How can I sort it by month name so that it comes in dropdown like Jan, Feb and so on.. 如何按月份名称对它进行排序,以便它出现在下拉菜单中,例如Jan,Feb等。
Below is the piece of code used 以下是所使用的代码
JSONArray jsonArray = new JSONArray(rowfielddata.getString("option_values"));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
f2 = f2 + "<option value='" + key + "'>" + json.get(key) + "</option>";
}
}
Thanks in advance!! 提前致谢!!
Perhaps you can change the part that iterates over the keys by the following solution: 也许您可以通过以下解决方案更改对键进行迭代的部分:
If you have a single object with the months as keys you may need an aditional array that maps the month as a number to the key of the single object. 如果您有一个以月为键的对象,则可能需要一个附加数组,将月份作为数字映射到该对象的键。
String [] monthsMapping = new String[12];
monthsMapping[0] = "January";
...
monthsMapping[11] = "December";
Then you genereate the options looping through this array: 然后生成遍历此数组的选项:
for(int i=0; i<12; i++){
//Generate the 12 options
... + singleMonthJSONObject.getString(monthsMapping[i]) + ...;
}
To me your input json looks to be incorrect, if you can modify the JSON to below structure: 对我来说,如果您可以将JSON修改为以下结构,则您输入的json看起来不正确:
[{"monthName": "January","order":1},
{"monthName": "February","order":2},
{"monthName": "March","order":3},
{"monthName": "April","order":4},
{"monthName": "May","order":5},
{"monthName": "June","order":6},
{"monthName": "July","order":7},
{"monthName": "August","order":8},
{"monthName": "September","order":9},
{"monthName": "October","order":10},
{"monthName": "November","order":11},
{"monthName": "December","order":12}]
Above JSON will give you array Now next you need to do after reading data in JSONArray sort the data based on order value 上面的JSON将为您提供数组现在,接下来您需要在读取JSONArray中的数据后根据订单值对数据进行排序
JSONArray jsonArray = jsonResult.getJSONArray("getalldata");
JSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonValues = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArr.length(); i++) {
jsonValues.add(jsonArr.getJSONObject(i));
}
Collections.sort( jsonValues, new Comparator<JSONObject>() {
private static final String KEY_NAME = "order";
@Override
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get(KEY_NAME);
valB = (String) b.get(KEY_NAME);
}
catch (JSONException e) {
//do something
}
return valA.compareTo(valB);
}
});
for (int i = 0; i < jsonValues.length(); i++) {
JSONObject jsonObj= jsonValues.getJSONObject(i);
f2 = f2 + "<option value='" +
jsonObj.optString("id")+ "'>" + jsonObj.optString("monthName") + "
</option>";
}
I assume there is another attribute also in the json with name id
as this is being used to assign value in option tag 我假设json中还有一个名为
id
属性,因为这用于在选项标签中分配值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.