简体   繁体   English

Json解析,字典值打印

[英]Json parsing, dictionary value print

I want to get the whatsapp number from JSON data which is in format as below 我想从JSON数据中获取whatsapp号,其格式如下

Json: " extra_values":"{\\"whatsapp\\":\\"1234567890\\",\\"call\\":\\"+1234567890\\"}" Json: “ extra_values”:“ {\\” whatsapp \\“:\\” 1234567890 \\“,\\” call \\“:\\” + 1234567890 \\“}”

    social_dict=[shop_info valueForKey:@"extra_values"];
    NSLog(@"%@",social_dict);//it_prints : {"whatsapp":"1234567890","call":"+1234567890"}


    NSLog(@"%@",[social_dict objectForKey:@"call"]);

    NSLog(@"%@",[social_dict objectForKey:@"whatsapp"]);

I am having problem in printing the whatsapp value. 我在打印whatsapp值时遇到问题。

Error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x60400028bd10' 错误: ***由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[__ NSCFString objectForKey:]:无法识别的选择器已发送到实例0x60400028bd10”

Try this 尝试这个

NSString *social_str = [shop_info valueForKey:@"extra_values"];

NSError *jsonError;
NSData *objectData = [social_str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * social_dict = [NSJSONSerialization JSONObjectWithData:objectData
                                      options:NSJSONReadingMutableContainers 
                                        error:&jsonError];

You are getting JSON string for extra_values so you have to convert it to NSDictionary by this 您正在获取extra_values JSON字符串,因此您必须通过此将其转换为NSDictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM