如何分别获取id和message的值

时间:2013-08-27 07:30:32

标签: iphone ios

我有NSString回复

  {"id":"44","message":"1"} , 

我想获得

的价值
"id" 

"message" 

分开。它是来自服务器的响应NSString

4 个答案:

答案 0 :(得分:3)

试试这个。

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

它会成功。

答案 1 :(得分:0)

你可以得到像

这样的回复
NSData *data = [stringResponse dataUsingEncoding:NSUTF8StringEncoding];
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", [result objectForKey:@"id"]);

希望这会有所帮助......

答案 2 :(得分:0)

NSData *data = [stringResponse dataUsingEncoding:NSUTF8StringEncoding];
//instead of id use NSDictionary
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", [result objectForKey:@"id"]);

答案 3 :(得分:0)

你可以试试这个:

NSDictionary *dic=[response JSONValue];
NSDictionary *id=[dic objectForKey:@"id"];
NSString * message =[dic objectForKey:@"message"];
 NSLog(@"id :%@   Message:%@ ",id,message);
祝你好运!