gpt4 book ai didi

ios - 如何从此JSON字典中检索数据?

转载 作者:行者123 更新时间:2023-12-01 17:53:32 27 4
gpt4 key购买 nike

所以我有一个Json字典数据如下

{
merchant = {
StoreID = 113;
};
token = {
Application = Merchant;
DatabaseID = 113;
DeviceToken = 9e7737daf0cb1e0572db47f520797adefd0367f66d5d6056f889732dcdb88772;
ID = 84;
LoginToken = "00000000-0000-0000-0000-000000000000";
OS = iOS;
};
}

数据只是作为“ token ”部分输入的,我将使用以下代码获取数据:
[userdefaults setObject:[json objectForKey:@"LoginToken"] forKey:@"loginToken"];

但是,数据已被修改为两个包装,我无法获取正确的数据。例如,如何从新数据中获取“storeID”?

最佳答案

您是否已经将json-string转换为NSDictionary?

NSString *storeID = [(NSDictionary *)[json objectForKey:@"merchant"] objectForKey:@"StoreID"];

如果不是,则需要先将其转换:
NSString *json_string = @"{\"merchant\": {\"StoreID\":\"123\"} }";
NSError *error;
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [json_string dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];

NSString storeID = JSON[@"merchant"][@"StoreID"];
NSLog(@"storeID: %@", storeID);

// Log output: storeID: 123

关于ios - 如何从此JSON字典中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545961/

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