从Objective-C中的字符串中检索特定数据

时间:2015-01-26 03:55:33

标签: objective-c string

我试图从字符串和字符串中检索一个整数

NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];

这会返回一个如下所示的字符串:

{"customer_id":0,"message":"The account is not existed."}

我想在两个不同的地方检索customer_id和消息

NSInteger *int = [requestReply objectForKey:@"customer_id"];
NSString *msg = [requestReply objectForKey:@"message"];

如何检索整数和消息?示例

1 个答案:

答案 0 :(得分:1)

看起来像JSON,你可以这样做:

id json = [NSJSONSerialization JSONObjectWithData:[requestReply dataWithEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSNumber *customerId = [json objectForKey:@"customer_id"];
NSString *msg = [json objectForKey:@"message"];