In Java I have:
String params = \"depCity=PAR&roomType=D&depCity=NYC\";
I want to get values of depCity parameters (PA
same but with jsonobject:
public static JSONObject getQueryParams2(String url) {
JSONObject json = new JSONObject();
try {
String[] urlParts = url.split("\\?");
JSONArray array = new JSONArray();
if (urlParts.length > 1) {
String query = urlParts[1];
for (String param : query.split("&")) {
String[] pair = param.split("=");
String key = URLDecoder.decode(pair[0], "UTF-8");
String value = "";
if (pair.length > 1) {
value = URLDecoder.decode(pair[1], "UTF-8");
if(json.has(key)) {
array = json.getJSONArray(key);
array.put(value);
json.put(key, array);
array = new JSONArray();
} else {
array.put(value);
json.put(key, array);
array = new JSONArray();
}
}
}
}
return json;
} catch (Exception ex) {
throw new AssertionError(ex);
}
}