json在ios 7中解析数组存储在数组中

时间:2014-09-10 05:54:58

标签: ios iphone json nsarray

这是我从API获得的回复

 {
Result =     {
    Area =         (

        "Shah-e-Alam",
        Vastrapur,
        "Nava Vadaj",
        Ambavadi,
        "Elis Bridge",
        Ranip,
        Gota,

    );
};
Status = Success;
 }

现在我需要在数组中只存储7个isaname如何存储它? 我的代码在这里

      if ([[res valueForKey:@"Status"]isEqualToString:@"Success"]) {
         NSLog(@"%@",res);
         arrAreaname = [[res valueForKey:@"Result"] mutableCopy];

           arrarea=[[arrAreaname valueForKey:@"Area"]mutableCopy];
                 NSLog(@"arrarea: %@", arrarea);

          NSData *json = [NSJSONSerialization dataWithJSONObject:arrarea      
             options:NSJSONWritingPrettyPrinted error:nil];
        NSLog(@"JSON: %@", json);

            NSString *jsonString = [[NSString alloc] initWithData:json 
             encoding:NSUTF8StringEncoding];

       NSLog(@"JSON string: %@", jsonString);



   }else{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"ERROR"
        message:@"connection error"
            delegate:nil
            cancelButtonTitle:@"OK"
        otherButtonTitles:nil];

    [message show];
  }

我现在到了这里jasonstring现在如何将数据存储在数组中?

3 个答案:

答案 0 :(得分:1)

我认为您的arrAreanameNSArray。仔细查看您的数据:

Result =     {
    Area =         (

        "Shah-e-Alam",
        Vastrapur,
        "Nava Vadaj",
        Ambavadi,
        "Elis Bridge",
        Ranip,
        Gota,

    );
};

结果的值为NSDictionary,关键区域的值为NSArray

修改

你应该这样做:

arrarea= [NSArray arrayWithArray: res[@"Result"][@"Area"]];

答案 1 :(得分:0)

这是可以帮助你的示例逻辑

NSArray *are=[[NSArray alloc] initWithObjects:@"Shah-e-Alam",
                  @"Vastrapur",
                  @"Nava Vadaj",
                  @"Ambavadi",
                  @"Elis Bridge",
                  @"Ranip",
                  @"Gota", nil];
    NSDictionary *dic2=[[NSDictionary alloc]initWithObjectsAndKeys:are,@"area", nil];
    NSMutableDictionary *dic1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:dic2,@"Result", nil];
    NSLog(@"dic1=%@",dic1);

    NSArray *array=[[NSArray alloc]init];

    array= [NSArray arrayWithArray:[NSArray arrayWithArray:[[NSMutableDictionary dictionaryWithDictionary:[dic1 valueForKey:@"Result"]] valueForKey:@"area"]]];

     NSLog(@"array=%@",array);

答案 2 :(得分:0)

您正在将字典写入json数据,而不是阅读。

// Use following lines to read JSON response.

NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:<json data> options:NSJSONReadingAllowFragments error:nil];
NSArray *areas = [NSArray arrayWithArray:dictResult[@"Result"][@"Area"]];
NSLog(@"Areas : %@ %@",dictResult[@"Result"][@"Area"],areas);