ios - 如何从ios中的raw中以漂亮的方式打印响应请求

标签 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":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb838a98ab819d8581c58884" rel="noreferrer noopener nofollow">[email protected]</a>","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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1c8c3c8d8f1c4c3d89fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a>",

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

提前致谢

最佳答案

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

在我的代码中,我传递的选项为零。因此,如果您传递选项为零,您就会获得不可变的容器

It means that all arrays will be NSArrays, all Dictionaries will be NSDictionaries, all strings will be NSStrings, and so on, even if the original objects were mutable dictionaries, dictionaries, or strings.

一般

Immutable objects are generally safer (no need to worry about a value changing underneath you)

Serialization

关于ios - 如何从ios中的raw中以漂亮的方式打印响应请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39093905/

相关文章:

ios - 最新 iOS 更新 (12.2) 后,来自本地存储的视频无法在 WebView (Xamarin.Forms) 中播放

ios - 解析100GB文件存储限制

ios - 突出显示单元格以进行多项选择

ios - 有什么简单的方法可以在 objective-c 中用一位数舍入 float 吗?

ios - 通过开发人员获取更多应用

ios - 缓存 Storyboard之间的 View Controller

javascript - PHP 数组 JSON 编码和 ExtJ 内的对象解码

javascript - Angularjs - 从 JSON 实例化数组

php - Json 变量值丢失

iOS 位置权限无法通过 Objective-C 显示