0

I am getting the following response
param=%7B%22paymentMode%22:%22%22,%22transactionId%22:%2231674%22,%22pgRespCode%22:%223%22,%22TxMsg%22:%22Canceled%20by%20user%22,%22authIdCode%22:%22%22,%22currency%22:%22INR%22,%22amount%22:%221.00%22,%22addressStreet1%22:%22Sesame%20street%22,%22addressStreet2%22:%22%22,%22isCOD%22:%22%22,%22loadStatus%22:%22fail%22,%22TxId%22:%22123456%22,%22addressCountry%22:%22India%22,%22firstName%22:%22Ankur%22,%22TxGateway%22:%22%22,%22signature%22:%2245558eb93513aa7a4f2fba24e0ba577b26eb5f40%22,%22addressState%22:%22Pune%22,%22lastName%22:%22Arya%22,%22addressCity%22:%22%22,%22TxRefNo%22:%22CTX1307151506338704178%22,%22loadAmount%22:%221.00%20INR%22,%22pgTxnNo%22:%22CTX1307151506338704178%22,%22TxStatus%22:%22CANCELED%22,%22email%22:%22daredevil.suyash@gmail.com%22,%22issuerRefNo%22:%22%22,%22mobileNo%22:%229900414420%22,%22addressZip%22:%22411045%22%7D

how can I decode it into the following

param={"paymentMode":"","transactionId":"31674","pgRespCode":"3","TxMsg":"Canceled by user","authIdCode":"","currency":"INR","amount":"1.00","addressStreet1":"Sesame street","addressStreet2":"","isCOD":"","loadStatus":"fail","TxId":"123456","addressCountry":"India","firstName":"Ankur","TxGateway":"","signature":"45558eb93513aa7a4f2fba24e0ba577b26eb5f40","addressState":"Pune","lastName":"Arya","addressCity":"","TxRefNo":"CTX1307151506338704178","loadAmount":"1.00 INR","pgTxnNo":"CTX1307151506338704178","TxStatus":"CANCELED","email":"daredevil.suyash@gmail.com","issuerRefNo":"","mobileNo":"9900414420","addressZip":"411045"}
Ankur Arya
  • 4,693
  • 5
  • 29
  • 50
  • Receiving a response which seems to be URL encoded JSON - is at least extremely unusual and unnecessary. You are sure you didn't get this due to a bug in the web service? – CouchDeveloper Jul 15 '13 at 16:30
  • It might be a bug in web service but the developer told me to decode it. If this is not right I can ask him to give a better response. – Ankur Arya Jul 15 '13 at 17:47
  • Yes, the most appropriate content-type for JSON data would be `Content-Type: application/json;charset=utf-8`. You might request this type explicitly through specifying the accept header in your request accordingly: `Accept: application/json;charset=utf-8` – CouchDeveloper Jul 15 '13 at 18:00

2 Answers2

3

You can try this method stringByReplacingPercentEscapesUsingEncoding:NSStringEncodingConversionAllowLossy]

Taken from this question urldecode in objective-c //

NSString *param=@"%7B%22paymentMode%22:%22%22,%22transactionId%22:%2231674%22,%22pgRespCode%22:%223%22,%22TxMsg%22:%22Canceled%20by%20user%22,%22authIdCode%22:%22%22,%22currency%22:%22INR%22,%22amount%22:%221.00%22,%22addressStreet1%22:%22Sesame%20street%22,%22addressStreet2%22:%22%22,%22isCOD%22:%22%22,%22loadStatus%22:%22fail%22,%22TxId%22:%22123456%22,%22addressCountry%22:%22India%22,%22firstName%22:%22Ankur%22,%22TxGateway%22:%22%22,%22signature%22:%2245558eb93513aa7a4f2fba24e0ba577b26eb5f40%22,%22addressState%22:%22Pune%22,%22lastName%22:%22Arya%22,%22addressCity%22:%22%22,%22TxRefNo%22:%22CTX1307151506338704178%22,%22loadAmount%22:%221.00%20INR%22,%22pgTxnNo%22:%22CTX1307151506338704178%22,%22TxStatus%22:%22CANCELED%22,%22email%22:%22daredevil.suyash@gmail.com%22,%22issuerRefNo%22:%22%22,%22mobileNo%22:%229900414420%22,%22addressZip%22:%22411045%22%7D";
    NSString *newParam = [param stringByReplacingPercentEscapesUsingEncoding:NSStringEncodingConversionAllowLossy];
    NSLog(@"%@",newParam);
Community
  • 1
  • 1
Yan
  • 3,533
  • 4
  • 24
  • 45
  • My only concern with this Yan, is that when you go to compile this, the compiler will likely have a problem with the % symbols in the `param` string. It will try to interpret them as escape characters, and cause a problem. You would need %% instead of % – MZimmerman6 Jul 15 '13 at 15:34
  • Of course the param string is just for example, but just a quick note about it :) – MZimmerman6 Jul 15 '13 at 15:34
  • I ran it in xcode and didn't get any errors. It interpreted the whole line as string. The only thing i am not sure about is the encoding parameter. I just picked one out of two :) I guess you should use the one that was mentioned in the answer NSUTF8StringEncoding – Yan Jul 15 '13 at 15:37
  • NSUTF8StringEncoding would likely work best. But as long as things are coming through consistently, no need to be concerned. – MZimmerman6 Jul 15 '13 at 15:44
1

Do string replacement stuff with that to get it into a more readable form.

string = [string stringByReplacingOccurrencesOfString: @"%%22" withString:@"\""];
string = [string stringByReplacingOccurrencesOfString: @"%%7B" withString:@"{"];
string = [string stringByReplacingOccurrencesOfString: @"%%7D" withString:@"}"];

And so on, until you get it into something you want.

You are basically replacing the unicode representation of the character into the actual readable character

MZimmerman6
  • 8,445
  • 10
  • 40
  • 70