从Webservices获取数据的问题

时间:2015-04-23 10:15:22

标签: objective-c iphone xcode web-services ios8

这里我从WebServices获取数据,但我需要单独的密钥数据。以下格式是我在WebServices上获得的。

 {
    aps =     {
        alert = "{\"user_id\":\"92\",\"ref_id\":\"58\",\"ref_name\":\"name\",\"date\":\"2015-04-23\",\"time\":\"16:27\",\"lat\":\"17.409769\",\"lon\":\"78.460849\",\"msg\":\"text message here\"}";
        sound = default;
    };

这里我得到NSMutableDictionary

  NSMutableDictionary*  json =[userInfo valueForKey:@"aps"];
        NSLog(@"%@",json);

在json上我得到了,

     {
         alert = "{\"user_id\":\"92\",\"ref_id\":\"58\",\"ref_name\":\"name\",\"date\":\"2015-04-23\",\"time\":\"16:27\",\"lat\":\"17.409769\",\"lon\":\"78.460849\",\"msg\":\"text message here\"}";
  sound = default;
}

这里得到alert

 NSDictionary* json=[json valueForKey:@"alert"];
        NSLog(@"%@",json);

现在我得到这样的回复,

{
        "user_id": "92",
        "ref_id": "58",
        "ref_name": "Name",
        "date": "2015-04-23",
        "time": "15:30",
        "lat": "17.409769",
        "lon": "78.460849",
        "msg": "Text message here"
    }

但我会尝试获取msg密钥,这里我开发了这样的代码,

  NSString *msg=[json objectForKey:@"msg"];
            NSLog(@"%@",msg);

问题我没有获得msg密钥,错误消息是

reason: '[<__NSCFString 0x15e4ab10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key msg.'

我如何从WebServices获得NSCFString,请您建议我如何在这里,谢谢。

3 个答案:

答案 0 :(得分:0)

使用此代码..

NSString *str = @"{\"Json\":{\"user_id\":\"92\",\"ref_id\":\"58\",\"ref_name\": \"Name\",\"date\": \"2015-04-23\",\"time\": \"15:30\",\"lat\": \"17.409769\",\"lon\":\"78.460849\",\"msg\": \"Text message here\"}}";
NSError *jsonError;
NSData *objectData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                     options:NSJSONReadingMutableContainers
                                                       error:&jsonError];
NSLog(@"%@",[json objectForKey:@"Json"]);

NSDictionary *dict = [json objectForKey:@"Json"];

NSLog(@"%@",[dict objectForKey:@"msg"]);

NSString *mesg =[dict objectForKey:@"msg"];

NSLog(@"%@", mesg);

答案 1 :(得分:-2)

如果你想打印msg只需使用你的代码

NSString* mesg=[Json valueForKey:@"msg"];
    NSLog(@"%@",mesg);

或者告诉我你想要做什么?

答案 2 :(得分:-2)

你做错了实际尝试以下。

NSData *data =  "Your response data";
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&error];

现在检查&#34; jsonDict&#34;值,告诉?