Iterate Json data from web service in correct order Iterate Json data from web service in correct order json json

Iterate Json data from web service in correct order


That's not how JSON works. It assumes order doesn't matter unless you have an array. If you want order to matter, you're using the wrong format- you need yo use an array of times->values rather than just times->value.

For your particular application, you can get the set of keys, then sort them with a custom comparator that uses the value parsed as a time to order them. But the built in libraries won't do it for you, because you aren't using JSON as its designed.


Vikas, as far as i understood your problem i managed to retrieve json object in expected format, hope this will help you. Enjoy!!

   String s = "{ \"15:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},\"16:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},\"17:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},\"18:00\":{\"type\":1,\"status\":\"1\",\"appointment_id\":5}}";    try {        JSONObject jobj = new JSONObject(s);        Iterator iter = jobj.keys();        while(iter.hasNext()){               String appointmentTime = String.valueOf(iter.next());            aRRaY.add(appointmentTime);     }      Collections.sort(aRRaY);       for(String key : aRRaY){          JSONObject appointmentDetails = jobj.getJSONObject(key);          System.out.println(key +" ----- "+appointmentDetails);      }    }    catch (JSONException e) {        e.printStackTrace();    }


I don't know the implementation you have done, and what 3pp you involved. But anyway, try below codes, you would get what you want.

import java.io.IOException;import java.util.LinkedHashMap;import java.util.Map.Entry;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;public class Test {    private static final ObjectMapper mapper = new ObjectMapper();    public static void main(String[] args) {        String json = "{"                + "\"15:00\":{\"type\":1,\"status\":null,\"appointment_id\":null}, "                + "\"16:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},"                + "\"17:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},"                + "\"18:00\":{\"type\":1,\"status\":\"1\",\"appointment_id\":5}"            + "}";        LinkedHashMap<String, LinkedHashMap<String, Integer>> fromJSON = fromJSON(            json, LinkedHashMap.class);        for (Entry<String, LinkedHashMap<String, Integer>> entry : fromJSON            .entrySet()) {            System.out.print(entry.getKey());            System.out                .println(" || status = " + entry.getValue().get("status"));        }    }    public static <T> T fromJSON(String input, Class<T> clazz) {        try {            return mapper.readValue(input != null ? input : "null", clazz);        } catch (JsonParseException e) {            throw new RuntimeException(e);        } catch (JsonMappingException e) {            throw new RuntimeException(e);        } catch (IOException e) {            throw new RuntimeException(e);        }    }}