Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I got a response on a url
Once I read the response i get this string as below

id=657ec95c439eb4a76e48c5ea7&amount=1&notify_status=success&currency=USD&time=20190403095815&reference=190403175800PTM03428&notify_id=D0000036639-a6d98e5d0a08d3e8850f&fields=id%2Camount%2Cnotify_status%2Ccurrency%2Ctime%2Creference%2Cnotify_id&sign=997b961b0541fb64b4c31096bc059055

I need to short this string like below
amount=1&currency=USD& fields=id%2Camount%2Cnotify_status%2Ccurrency%2Ctime%2Creference%2Cnotify_id&id=657ec95c439eb4a76e48c5ea7&notify_id=D0000036639-a6d98e5d0a08d3e8850f &notify_status=success&reference=190403175800PTM03428&sign=997b961b0541fb64b4c31096bc059055&time=20190403095815

Basically, I need short by keys and my string is like key1=value1&key2=value2&key3=value3

Appreciate any Help

What I have tried:

string   requestJson = "id=657ec95c439eb4a76e48c5ea7&amount=1&notify_status=success&currency=USD&time=20190403095815&reference=190403175800PTM03428&notify_id=D0000036639-a6d98e5d0a08d3e8850f&fields=id%2Camount%2Cnotify_status%2Ccurrency%2Ctime%2Creference%2Cnotify_id&sign=997b961b0541fb64b4c31096bc059055";

            var list = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(requestJson);



            SortedDictionary<string, string> parameters = new SortedDictionary<string, string>();
            foreach (var term in list)
            {
                if (term.Key == "sign")
                    continue;

                parameters.Add(term.Key, term.Value);
            }
Posted
Updated 3-Apr-19 23:21pm
v4
Comments
Mehdi Gholam 4-Apr-19 5:19am    
Why?
Keith Barrow 4-Apr-19 5:30am    
The JavaScriptSerializer.Deserialize method isn't going to deserialize the query parameters - it is expecting a JSON string. OriginalGriff's solution is where I'd start.

Also Mehdi Gholam asks a good question - I can't think of a good reason why you'd want to do this other than as an exercise. That doesn't mean there isn't a good reason, but its good to question "am I actually solving the correct problem".

1 solution

Take your string, and use Split, then sort the array:
string query = @"id = 657ec95c439eb4a76e48c5ea7 & amount = 1 & notify_status = success & currency = USD & time = 20190403095815 & reference = 190403175800PTM03428 & notify_id = D0000036639 - a6d98e5d0a08d3e8850f & fields = id % 2Camount % 2Cnotify_status % 2Ccurrency % 2Ctime % 2Creference % 2Cnotify_id & sign = 997b961b0541fb64b4c31096bc059055";
string trimmed = query.Replace(" & ", "&");
string[] queries = trimmed.Split('&');
queries = queries.Select(q => q.Trim()).OrderBy(q => q).ToArray();
If it wasn't for the spaces around the '&' in your original string (which I think are probably spurious) you could just use Array.Sort:
string query = @"id = 657ec95c439eb4a76e48c5ea7 & amount = 1 & notify_status = success & currency = USD & time = 20190403095815 & reference = 190403175800PTM03428 & notify_id = D0000036639 - a6d98e5d0a08d3e8850f & fields = id % 2Camount % 2Cnotify_status % 2Ccurrency % 2Ctime % 2Creference % 2Cnotify_id & sign = 997b961b0541fb64b4c31096bc059055";
string[] queries = query.Split('&');
Array.Sort(queries);
 
Share this answer
 
v2
Comments
MadMyche 4-Apr-19 6:55am    
It does look like the spaces are spurious; usually they would be escaped as %20. Probably should throw in a url.decode
OriginalGriff 4-Apr-19 7:04am    
Or he typed it instead of copy'n'paste
MadMyche 4-Apr-19 7:23am    
Aah programmers.. copy and paste what they don't understand, and then don't copy and paste what we need to know.
OriginalGriff 4-Apr-19 7:32am    
I do wonder sometimes ... and try not to use airplanes any more ... :laugh:
AtulSharma609 4-Apr-19 7:57am    
Thank you so much for a quick answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900