我有一个带有单个对象的json数组,其中key作为月份名称。从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>
如何按月份名称对其进行排序,以便按照Jan,Feb等下拉列表进行排序..
以下是使用的代码
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>";
}
}
提前致谢!!
答案 0 :(得分:0)
也许您可以通过以下解决方案更改迭代密钥的部分:
如果您有一个单独的月份作为键的对象,您可能需要一个aditional数组,将月份作为数字映射到单个对象的键。
String [] monthsMapping = new String[12];
monthsMapping[0] = "January";
...
monthsMapping[11] = "December";
然后你创建循环遍历这个数组的选项:
for(int i=0; i<12; i++){
//Generate the 12 options
... + singleMonthJSONObject.getString(monthsMapping[i]) + ...;
}
答案 1 :(得分:0)
对我来说,如果您可以将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}]
以上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>";
}
我假设json中还有另一个名为id
的属性,因为它用于在选项标签中分配值