gpt4 book ai didi

objective-c - 使用 Objective-C 的 JSON 框架解析嵌套的 JSON 对象

转载 作者:太空狗 更新时间:2023-10-30 03:40:01 25 4
gpt4 key购买 nike

我有以下 JSON 对象:

{
"response": {
"status": 200
},
"messages": [
{
"message": {
"user": "value"
"pass": "value",
"url": "value"
}
]
}

我正在使用 JSON-Framework(也尝试过 JSON Touch)来解析它并创建一个字典。我想访问“消息” block 并提取“用户”、“传递”和“url”值。

在 Obj-C 中,我有以下代码:

// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

//Print contents of json-string
NSArray *statuses = [parser objectWithString:json_string error:nil];
NSLog(@"Array Contents: %@", [statuses valueForKey:@"messages"]);
NSLog(@"Array Count: %d", [statuses count]);


NSDictionary *results = [json_string JSONValue];
NSArray *tweets = [[results objectForKey:@"messages"] objectForKey:@"message"];

for (NSDictionary *tweet in tweets)
{
NSString *url = [tweet objectForKey:@"url"];
NSLog(@"url is: %@",url);
}

我可以提取“消息”并查看所有“消息” block ,但我无法更深入地解析并提取“用户”、“密码”和“url”。

最佳答案

已解决:

NSArray *tweets = [[results objectForKey:@"messages"] valueForKey:@"message"];

关于objective-c - 使用 Objective-C 的 JSON 框架解析嵌套的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2414426/

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