Ich erhalte diese Zeichenfolge (von webservice) in eine JSONArray,Java- Verarbeitung Json-Array auf Schlüsseln basierend
[{
"textinput": [{
"position": 0,
"dependency": "no",
"id": 0,
"Itype": "textinput"
}, {
"position": 2,
"dependency": "no",
"id": 1,
"Itype": "textinput"
}]
}, {
"textarea": [{
"position": 1,
"type": "textarea",
"dependency": "no",
"id": 0
}]
}]
Ich brauche das Array zu sortieren, indem Reihenfolge basierend auf Tasten- „Position“ ich aufsteigend der folgende Code bin mit org.json Bibliothek, ist der, so weit der Code i
verwendetJSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArray.length(); i++) {
jsonList.add(jsonArray.getJSONObject(i));
}
Collections.sort(jsonList, new Comparator<JSONObject>() {
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get("position");
valB = (String) b.get("position");
}
catch (JSONException e) {
//do something
}
return valA.compareTo(valB);
}
});
for (int i = 0; i < jsonArray.length(); i++) {
sortedJsonArray.put(jsonList.get(i));
}
AALso andere Links auf der Website versucht. bitte helfen
Verwendung von PHP ein Array zu sortieren, die sehr einfach – vinoth12594
ist, was der Fehler ist, dass Sie bekommen? Möchten Sie diesen Code optimieren? –
@ vinoth12594 danke. aber ich kann nicht, weil das sortierte Array an eine Android-Anwendung weitergegeben werden muss –