我有NSString
回应
{"id":"44","message":"1"} ,
我想获得的价值
"id"
和
"message"
分别地。NSString
这是来自服务器的响应。
试试这个。
NSData *data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSDictionary dictionaryWithDictionary=:[NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableLeaves error:&error]];
NSLog(@"id:%@", [dict objectForKey:@"id"]);
NSLog(@"message:%@", [dict objectForKey:@"message"]);
它会解决的。
那么你可以得到这样的回应
NSData *data = [stringResponse dataUsingEncoding:NSUTF8StringEncoding];
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", [result objectForKey:@"id"]);
希望这可以帮助...
NSData *data = [stringResponse dataUsingEncoding:NSUTF8StringEncoding];
//instead of id use NSDictionary
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", [result objectForKey:@"id"]);
你可以试试这个:
NSDictionary *dic=[response JSONValue];
NSDictionary *id=[dic objectForKey:@"id"];
NSString * message =[dic objectForKey:@"message"];
NSLog(@"id :%@ Message:%@ ",id,message);
祝你好运!