0

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

}

我不能更进一步吗?

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

任何帮助将不胜感激。

4

2 回答 2

1

解析 JSON 的基础是:

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

因此,在您的中,根部JSON有 a ,并且在其内部有一个包含a 的键。dictionaryapplicationArray

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];
于 2013-08-06T10:25:28.577 回答
0

我认为这不是 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 格式进行响应。

于 2013-08-06T10:30:40.310 回答