如何从ios中的raw生成漂亮的响应请求

时间:2016-08-23 06:33:32

标签: ios objective-c json cocoa-touch http-post

我正以这种方式退出

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

我想以这种方式输出

{

"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
  }
}

我的代码是

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];

提前致谢

2 个答案:

答案 0 :(得分:2)

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];

答案 1 :(得分:2)

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
                                  options:0 
                                    error:&jsonError];
NSArray *keys=[json allKeys];
NSArray *values=[json allValues];
NSString *milliseconds = [NSString stringWithFormat:@"%@",[[json objectForKey:@"consumer"] objectForKey:@"date-joined"]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:([milliseconds longLongValue] / 1000)];
NSDateFormatter *datfrm = [[NSDateFormatter alloc] init];
[datfrm setDateFormat:@"dd/MM/yyyy"];
NSString *strDate = [datfrm stringFromDate:date];
NSLog(@"The date is - %@",strDate);

打印结果是

The date is - 23/08/2016

在我的代码中,我传递的选项为零。如果您将选项作为零传递获取不可变容器

  

这意味着所有数组都是NSArrays,所有字典都是NSDictionaries,所有字符串都是NSStrings,依此类推,即使原始对象是可变的字典,字典或字符串。

一般

  

不可变对象通常更安全(无需担心您下方的值会发生变化)

Serialization