Iterate Json data from web service in the correct order

I have a response coming from a web service, the data is in JSON form.

 JSONObject event:- { "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} } 

I do not know the key values, they are random. Therefore, when I repeat the data using an iterator, choosing keys and hasNext() . It returns data, but reorders the incoming data.

 Iterator AppoinmentIter = event.keys(); while(AppoinmentIter.hasNext()){ String appointmentTime = (String)AppoinmentIter.next(); JSONObject appointmentDetails = event.getJSONObject(appointmentTime); } 

I want the data in the exact order in which it goes. I checked this link, it suggests using LinkedHashMap . But here they insert the key value. And I do not know the keys in my data. So how can I iterate over the data in the correct order. Please, help..

+4
source share
3 answers

Vikas, as far as I understood your problem, I managed to return the json object in the expected format, I hope this helps 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(); } 
+1
source

This is not how JSON works. It assumes that order doesn't matter if you don't have an array. If you want the order to matter, you are using the wrong format - you need to use an array of time values->, not just time-> value.

For your specific application, you can get a set of keys, and then sort them using a specialized comparator, which uses the value analyzed as the time to order them. But the built-in libraries will not do this for you, because you are not using JSON as your design.

+2
source

I don’t know which implementation you made and which 3pp you used. But anyway, try under the codes, you will 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); } } } 
0
source

Source: https://habr.com/ru/post/1490209/


All Articles