-1

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&
gprathour
  • 14,813
  • 5
  • 66
  • 90
parking V
  • 5
  • 4

4 Answers4

1

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]);
    }
Aashit Shah
  • 578
  • 3
  • 9
0

This looks like a String with mixture of URL encoding (like %3A for :) and HTML characters (like &amp; 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");
gprathour
  • 14,813
  • 5
  • 66
  • 90
0

You can simply use String replaceAll() function to create the json.

str= str.replaceAll("=", "\"");
str= str.replaceAll("&amp;", "\",\"");
str= "{\""+ str+ "\"}";
Anirban Roy
  • 180
  • 6
0

It seems the string is both URL encoded (the %3A) and JSON encoded (the &amp;).

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 &amp;.

String payumoney = "addedon=2019-11-21+17%3A06%3A42&amp;productinfo=FINE&amp;firstname=Creataum+Test+User&amp;";

// String decoding.
payumoney = payumoney.replaceAll("%3A", "-");
payumoney = payumoney.replaceAll("&amp;", "&");

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]);
}
selalerer
  • 3,766
  • 2
  • 23
  • 33