gpt4 book ai didi

iphone - 如何解析 iphone 中的 json 响应?

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

我有一个应用程序,其中我有一个像这样的 json 响应:

{
"status":"ok",
"dfs":[{"type":"i",
"title":"image",
"image_path":"https:\/\/s3.dhgdfhgfhgdhgfh\/ad_226.png"}]
}

我是这样解析的:

NSDictionary *dict=[[request responseString] JSONValue];                
NSLog(@"dict %@ ",[request responseString]);

if([[dict objectForKey:@"status"] isEqualToString:@"ok"])
{
NSMutableDictionary *dict1=[dict objectForKey:@"dfs"];

NSLog(@"%@",[dict1 classForCoder]);
NSLog(@"%@dict1",dict1);
titlelbl.text=[dict1 objectForKey:@"title"];

if ([[dict1 objectForKey:@"type"] isEqualToString:@"i"])
{
ad.hidden=NO;

NSString *imageurl=[dict1 objectForKey:@"image_path"];

NSLog(@"%@",imageurl);
}

但是我得到这个错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xde7ecf0' .

我将 dict1 类文件夹作为 nsmutable 数组。谁能帮助我实现这一目标?

最佳答案

{
"status":"ok",
"dfs":
[
{
"type":"i",
"title":"image ",
"image_path":"https:\/\/s3.dhgdfhgfhgdhgfh\/ad_226.png"
}
]
}

读取上面的 json 响应,它是一个字典,其中第一个对象是字符串,第二个是字典数组(只有一个对象)。

NSDictionary *dict = [[request responseString] JSONValue];

if([[dict objectForKey:@"status"] isEqualToString:@"ok"])
{
NSArray* arr = [dict objectForKey:@"dfs"];
NSDictionary* dict1 = [arr objectAtIndex:0];
enter code here
enter code here
enter code here
enter code here

// get the data from dict1 with the key value pairs, and set labels and images
}

关于iphone - 如何解析 iphone 中的 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12489140/

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