gpt4 book ai didi

ios - Json解析、字典值打印

转载 作者:行者123 更新时间:2023-11-29 00:00:29 25 4
gpt4 key购买 nike

我想从格式如下的 JSON 数据中获取 whatsapp 号码

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

我在打印 whatsapp 值时遇到问题。

Error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x60400028bd10'

最佳答案

试试这个

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

您正在获取 extra_values 的 JSON 字符串,因此您必须通过此将其转换为 NSDictionary

关于ios - Json解析、字典值打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49610918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com