I have a JSON object which holds some json_array like this: (with org.codehaus.jettison.json.*)
json array:[{"C1":["S1","S2"]},{"C2":["S1","S2"]},{"C3":["S1","S2"]},{"C4":["S1","S2"]}]
I have to get two list,one for keys,another for the elements of each key.I have done like this:
//sample: clusterIdList:[C1, C2, C3, C4] and serviceIdList:[S1, S2, S1, S2, S1, S2, S1, S2]
//get jsonarray by key = serviceCode
JSONArray array = incident.getJSONArray("serviceCode");
List<String> clusterIdList = new ArrayList<String>();
List<String> serviceIdList = new ArrayList<String>();
String key="";
for (int i = 0,length=array.length(); i < length; i++) {
JSONObject b = array.getJSONObject(i);
key = b.names().getString(0);
clusterIdList.add(key);
JSONArray sublist = b.getJSONArray(key);
for (int j = 0,sublistLength = sublist.length(); j < sublistLength; j++) {
serviceIdList.add(sublist.getString(j));
}
}
Is this a proper way to do this? or can it be improved? I am trying to improve my code quality and readability.
key
into the loop:String key = b.names().getString(0);
\$\endgroup\$