vamsi vamsi - 3 months ago 10
JSON Question

how to print response request in pretty from raw in ios

I am getting out put in this way

2016-08-23 11:48:04.944 LogInPage[3340:45237] requestReply: {"account-token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjhmYjI3NDEwZGNkMjQ3ZTY5MTQ1YWE4ZGZhNzY1OWVlIiwiaWF0IjoxNDcxOTMzMDg0LCJleHAiOjE0NzE5MzQ4ODQsImF1ZCI6ImJhbmRvYnJhcy5yb2xsZm9yd2FyZC5uZXQiLCJpc3MiOiJSb2xsIEZvcndhcmQgTExDIiwic3ViIjoiYWNjb3VudDpjb25zdW1lcjpub3JtYWwifQ.4gkE6Qq1TXKEvU9wyBc3aS2L73866TwTxR0sC8mcqjg","consumer":{"id":"8fb27410dcd247e69145aa8dfa7659ee","date-joined":1471933084000,"reward-multiplier":0,"verified":false,"preferences":{"daily-journal":1,"followed":1,"bonus-rolls":1},"contacts":{"name":null,"phone":null}},"user":{"id":"33e4442e9fbc4188a50448864a0bdf20","email":"has@jvnj.co","password":null,"consumer-id":"8fb27410dcd247e69145aa8dfa7659ee","business-id":null,"admin-id":null}}16


I want output in this way

{

"account-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjQ0NTk1OTI2ZDRhYzQyMjc4OTliMzBjODQ4NWMxMGZkIiwiaWF0IjoxNDcxOTMwODg4LCJleHAiOjE0NzE5MzI2ODgsImF1ZCI6ImJhbmRvYnJhcy5yb2xsZm9yd2FyZC5uZXQiLCJpc3MiOiJSb2xsIEZvcndhcmQgTExDIiwic3ViIjoiYWNjb3VudDpjb25zdW1lcjpub3JtYWwifQ._PmzVQ4GfoVX2QPBi6wBwslCX-IWV3jUL1lqXihAqW4",

"consumer": {

"id": "44595926d4ac4227899b30c8485c10fd",

"date-joined": 1471930888000,

"reward-multiplier": 0,

"verified": false,

"preferences": {

"daily-journal": 1,


"followed": 1,
"bonus-rolls": 1
}
,
"contacts": {

"name": null,
"phone": null
}
},

"user": {

"id": "504559b7c57f4a0e9d6453ad18d713e6",

"email": "yryi@uri.com",

"password": null,

"consumer-id": "44595926d4ac4227899b30c8485c10fd",

"business-id": null,

"admin-id": null
}
}


My code is

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"requestReply: %@%@", requestReply,postLength);
// NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);

}] resume];


thanks in advance

Answer
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
                                      options:NSJSONReadingMutableContainers 
                                        error:&jsonError];
       NSLog(@"Response = %@",json);

    }] resume];