ios - 将存储在 NSDictionary 中的 JSON 数据中的应用程序名称存储在 NSArray 中

标签 ios json api nsarray nsdictionary

我以 JSON 格式检索了服务器的以下响应,并将其成功存储在 NSDictionary 中。

{
    "@companyName" = "MobileAPPSDeveloper";
    "@generatedDate" = "8/6/13 2:41 AM";
    "@version" = "1.0";
    application =     (

            {
        "@apiKey" = 234FXPQB36GHFF2334H;
        "@createdDate" = "2013-03-01";
        "@name" = FirstApp;
        "@platform" = iPhone;
    },
            {
        "@apiKey" = 3PF9WDY234546234M3Z;
        "@createdDate" = "2013-02-01";
        "@name" = SecondAPP;
        "@platform" = iPhone;
    },
            {
        "@apiKey" = NXGQXM2347Y23234Q4;
        "@createdDate" = "2013-05-22";
        "@name" = ThirdApp;
        "@platform" = Android;
    }
);

}

这是我用过的方法。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [self convertDataIntoDictionary:responseData];
}

-(void)convertDataIntoDictionary:(NSData*)data{
    NSDictionary *dictionary;
    if (data.length>0) {
        NSError *parsingDataError;
        dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parsingDataError];

        if (parsingDataError) {
            //            [ErrorAlert showError:parsingDataError];
            NSString *recievedString = [[NSString  alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Data Conversion Error When Parsing: %@", recievedString);
        }
    }

    NSLog(@"Data back from server\n %@",dictionary);
    NSArray *applicationDetails = dictionary[@"application"];

}

我不能再进一步了吗?

我想获取数组中应用程序的所有名称。

如有任何帮助,我们将不胜感激。

最佳答案

解析 JSON 的基础知识是:

{ }- dis represents dictionary
( )- dis represents array

因此,在您的 JSON 中,根部有一个 dictionary,其中有一个关键的 application,其中包含一个 Array .

NSArray *applicationArray = [dictionary objectForKey:@"application"];
NSMutableArray *mutableAppNamesArray = [[NSMutableArray alloc]init];

在这个数组中,每个索引处都有三个 NSDictionary:

for(NSDictionary *dict in applicationArray){
  NSString *appName = [dict objectForKey:@"@name"];
  NSLog(@"app name : %@", appName);
  [mutableAppNamesArray addObject:appName];
}

NSArray * appNamesArray = [NSArray arrayWithArray:mutableAppNamesArray];

关于ios - 将存储在 NSDictionary 中的 JSON 数据中的应用程序名称存储在 NSArray 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18077242/

相关文章:

ios - 以编程方式更改现有的 UIBlurEffect UIBlurEffectStyle?

JavaScript 数组到 java

api - 如何使用 postman 模拟服务器

c# - Json文件请求产生“(413)请求实体太大”。

ruby-on-rails - RABL 将 ActiveRecord 时间戳或日期时间呈现为数字而不是字符串

c - API 函数的返回值

bash - 命令行将APK上载到Google Play商店

javascript - React Native 中组件之间的通信(子 -> 父)

ios - swift xcode 播放播放器列表中的声音文件

ios - 原始值上的 KVC 集合运算符