问题
I have a response coming from a web service, 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 don't know the key values, they are random. So when i iterate the data using iterator by fetching keys
and hasNext()
. It returns the data but changes the order of coming data.
Iterator AppoinmentIter = event.keys();
while(AppoinmentIter.hasNext()){
String appointmentTime = (String)AppoinmentIter.next();
JSONObject appointmentDetails = event.getJSONObject(appointmentTime);
}
I want the data in exact order in which it is coming.
I checked this link, it suggests to use LinkedHashMap
. But here they are inserting the value by keys. And i don't know the keys in my data. So how can i iterate the data in correct order. Please help..
回答1:
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();
}
回答2:
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.
回答3:
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);
}
}
}
来源:https://stackoverflow.com/questions/17519769/iterate-json-data-from-web-service-in-correct-order