【问题标题】:Store App names in NSArray from JSON DATA which Stored in NSDictionary从存储在 NSDictionary 中的 JSON DATA 将应用程序名称存储在 NSArray 中
【发布时间】:2013-08-06 10:12:22
【问题描述】:

我以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"];

}

这样我就不能更进一步了?

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ios json api nsarray nsdictionary


    【解决方案1】:

    解析JSON的基础是:

    { }-dis 表示字典
    ( )-dis 表示数组

    所以在您的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];
    

    【讨论】:

    • 嗨 Puneet 感谢您的回答!我会记住这一点并尝试相应地转换数据。几分钟后更新!!
    • NSArray *appNamesArray = [[NSArray alloc]init]; NSArray 没有 addobject: 方法!!如果我更改为 NSMutableArray 它会给我 Error[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack:
    • 对不起,我的错。我没有使用 IDE。我已编辑。使用 NSMutableArray 而不是 NSArray。如果你需要 NSArray,你可以从 NSMutableArray 创建它。
    • 终于有答案了!!我也编辑了答案!谢谢普内特
    • 很高兴它成功了。我已将您的编辑包含在最终答案中。快乐编码:)
    【解决方案2】:

    我认为这不是 JSON 的正确格式。 JSON 对象通常包含 ':' 而不是 '='

    示例:-

    {
    products_id: "476",
    products_model: "XOLO Play",
    products_price: "15499.0000",
    products_image: "xolo-play-play-t1000-.jpg",
    products_date_available: null,
    products_last_modified: "2013-08-04 06:04:11",
    products_date_added: "2013-08-04 06:03:10",
    manufacturers_id: "17",
    products_status: "1",
    products_rating: null,
    category_id: "11"
    },
    {
    products_id: "18",
    products_model: "Nokia Lumia 820",
    products_price: "8990.0000",
    products_image: "samsung-galaxy-ace-duos-gsm-mobile-phone-large-1.jpg",
    products_date_available: null,
    products_last_modified: "2013-07-30 03:45:28",
    products_date_added: "2013-01-23 00:52:00",
    manufacturers_id: "2",
    products_status: "1",
    products_rating: "3",
    category_id: "11"
    }, 
    

    但如果你仍然需要解析它,那么你可以尝试如下:

    首先进入数组 通过NSDictionary *array = [allValues objectForKey:@"application"]; 然后每个索引到 NSDictionary 然后对象作为键。如果它是您的服务器响应而不是尝试使用正确的 JSON 格式。

    【讨论】:

    • 实际的服务器响应是:not =
    • 比@Puneet ans 肯定会帮助你:)
    猜你喜欢
    • 2011-03-10
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多