gpt4 book ai didi

ios - 如何获取JSON数据中的第一个对象名称?

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

我的服务器人员根据消息对象中的不同请求向我发送包含不同对象的 JSON 数据。比如,在一些请求中:

{
"message": {
"series": {...}
},
"boolean": true
}

在一些请求中:

{
"message": {
"movie": {...}
},
"boolean": true
}

而且两个对象都有不同的数据。所以,我想在消息对象中获取第一个对象的名称,它是“电影”或“系列”,这样我就可以对其进行 if else 条件以获取内部数据。

    NSString *myRequestString = [NSString stringWithFormat:@"getVideo=1"];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",response);

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];

// for(NSDictionary *dict in firstObject){
// NSLog(@"%@",[dict objectForKey:@"name"]);
// }

最佳答案

dict = [dict objectForKey:@"message"];
if([dict objectForKey:@"movie"]){
//Your dictionary has key movie, parse accordingly
}
else if ([dict objectForKey:@"series"]){
//Your dictionary has key series, parse accordingly
}

希望对你有帮助

关于ios - 如何获取JSON数据中的第一个对象名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31470232/

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