I got this data in PayUMoney
and to show the user But problem how to get this data in json
or key value pair format any one know that please help!!
addedon=2019-11-21+17%3A06%3A42&productinfo=FINE&firstname=Creataum+Test+User&
Assuming that u have got it as String following code will print the key value pair from the string
String x ="addedon=2019-11-21+17%3A06%3A42&productinfo=FINE&firstname=Creataum+Test+User&";
String pair[] = x.split("&");
Log.e("pairs", Arrays.asList(pair).toString());
for (int i=0;i<pair.length;i++){
String key[] = pair[i].split("=");
Log.e("pair:","key= "+ key[0]+" value= "+key[1]);
}
This looks like a String with mixture of URL encoding (like %3A
for :
) and HTML characters (like &
for &
).
I believe you can try decoding it and then split data like say by &
to get data for each key and then further split by =
to get value for that key.
For URL decoding you can try like
String decoded = java.net.URLDecoder.decode(url, "UTF-8");
You can simply use String replaceAll() function to create the json.
str= str.replaceAll("=", "\"");
str= str.replaceAll("&", "\",\"");
str= "{\""+ str+ "\"}";
It seems the string is both URL encoded (the %3A
) and JSON encoded (the &
).
You have to decode the string and then split by '&'
and then split each pair to key and value by '='
.
You can see here how to JSON decode: Decoding JSON String in Java
URL decoding can be done with Java's URLDecoder
class: https://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html
For this example I'll just assume the only encoded characters are %3A
and &
.
String payumoney = "addedon=2019-11-21+17%3A06%3A42&productinfo=FINE&firstname=Creataum+Test+User&";
// String decoding.
payumoney = payumoney.replaceAll("%3A", "-");
payumoney = payumoney.replaceAll("&", "&");
HashMap<String, String> params = new HashMap<String, String>();
String[] pairs = payumoney.split("&");
for (String pair : pairs) {
String[] keyValue = pair.split("=");
params.put(keyValue[0], keyValue[1]);
}