在iOS元素中存储可变大小的JSON数组

时间:2016-03-14 17:52:15

标签: ios objective-c json

{
  "bidstatus":[
        {"bidstatusid":"1","bidstatus":"Quoted"},
        {"bidstatusid":"2","bidstatus":"Closed"},
        {"bidstatusid":"3","bidstatus":"Awarded"}
        ],
  "config":{
        "adminfee":"100",
        "percentcommission":"10",
        "premiumfee":"0"
        },
  "lvlsubj":{
        "Primary 1":["English","Geography","Maths","Science"],
        "Primary 2":["English","Geography","History"],
        "Primary 3":["Maths","Science"]
        }
}

我有来自API的传入JSON回复,它返回上述内容。现有的iOS代码使用AFNetworking来解析JSON。我可以将bidstatus存储为NSArray并将其配置为NSDictionary。但是,我无法正确存储lvlsubj。该对象看起来像一个2D数组,其中第二级数组的大小可变。

我该如何解决?

现有代码非常简单。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString* strRequestLink = [NSString stringWithFormat:@"%@%@", SERVICEPATH, @"get_var.php"];
[manager GET: strRequestLink
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"JSON: %@", responseObject);
         [GlobalPool sharedObject].m_dictPaymentConfig = [[responseObject valueForKey:@"config"] mutableCopy];
         [GlobalPool sharedObject].m_arrayBidStatus = [[responseObject valueForKey:@"bidstatus"] mutableCopy];
         //[GlobalPool sharedObject].m_arrayLevelSubject = [[responseObject valueForKey:@"lvlsubj"] mutableCopy];

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);

         [self showServerConnectionError];
     }];

1 个答案:

答案 0 :(得分:0)

感谢上面的建议,我设法将它们存储起来并阅读。

将其存储为NSDictionary。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString* strRequestLink = [NSString stringWithFormat:@"%@%@", SERVICEPATH, @"get_var.php"];
[manager GET: strRequestLink
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"JSON: %@", responseObject);
         [GlobalPool sharedObject].m_dictPaymentConfig = [[responseObject valueForKey:@"config"] mutableCopy];
         [GlobalPool sharedObject].m_arrayBidStatus = [[responseObject valueForKey:@"bidstatus"] mutableCopy];
         [GlobalPool sharedObject].m_dictLevelSubject = [[responseObject valueForKey:@"lvlsubj"] mutableCopy];

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);

         [self showServerConnectionError];
     }];

然后使用allKeys在NSArray中存储密钥。使用密钥,从NSDictionary(将返回数组)中读取。

此处的代码使用键填充选择器。选择一个值后,它将从NSDictionary读取以获取相应的对象(NSArray)并填充第二个选择器。

- (IBAction)actionChooseLevel:(id)sender {
    NSArray* arrayKeys = [[GlobalPool sharedObject].m_dictLevelSubject allKeys];
    if (arrayKeys.count == 0)
        return;

    [arrayPickerItems removeAllObjects];
    for (int nIdx = 0; nIdx < arrayKeys.count; nIdx++)
    {
        NSMutableString* strInfo = [arrayKeys objectAtIndex:nIdx];
        [arrayPickerItems addObject:strInfo];
    }

    [ActionSheetStringPicker showPickerWithTitle:@"Choose Level" rows:arrayPickerItems initialSelection:nSelectedLevelIdx > 0 ? nSelectedLevelIdx : 0 target:self successAction:@selector(itemWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
}

- (IBAction)actionChooseSubject:(id)sender {
    NSArray* arrayObject = [[GlobalPool sharedObject].m_dictLevelSubject objectForKey:self.m_lblSelectedLevel.text];
    if (arrayObject.count == 0)
        return;

    [arrayPickerItems removeAllObjects];
    for (int nIdx = 0; nIdx < arrayObject.count; nIdx++)
    {
        NSMutableString* strInfo = [arrayObject objectAtIndex:nIdx];
        [arrayPickerItems addObject:strInfo];
    }

    [ActionSheetStringPicker showPickerWithTitle:@"Choose Subject" rows:arrayPickerItems initialSelection:nSelectedSubjectIdx > 0 ? nSelectedSubjectIdx : 0 target:self successAction:@selector(itemWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
}