从NSString ios获取日期

时间:2015-11-27 07:29:33

标签: ios objective-c

我的字符串包含::

 [{"temperature":"96.800003","date":"2015-11-26 18:44:42","random_id":"-1963708530","temp_type":"0","drugsymptom_value":""}]

要求: 我怎样才能从上面的字符串中获取日期值

2 个答案:

答案 0 :(得分:2)

<强>步骤1

将您的JSON字符串转换为简单字符串,就像

一样
 NSString *yourString = [{"temperature":"96.800003","date":"2015-11-26 18:44:42","random_id":"-1963708530","temp_type":"0","drugsymptom_value":""}];

 NSData *jsonData = [yourString dataUsingEncoding:NSUTF8StringEncoding];
 // your yourString is started with array not a Dictionary
NSArray *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:nil];

// now you have Date String
 NSString *DateSTring  = dict[0][@"date"];

<强>步骤-2

你的字符串为

 2015-11-26 18:44:42
 yyyy-MM-dd HH:mm:ss

 NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

 NSDate *finaldate=[dateFormatter dateFromString:DateSTring];


 NSLog(@" finaldate %@",finaldate);

答案 1 :(得分:-1)

我先用JSON格式化它,然后你就可以使用NSDateFormatter