从服务获取数据目标 c

Posted

技术标签:

【中文标题】从服务获取数据目标 c【英文标题】:Getting data from a services Objective c 【发布时间】:2017-04-13 08:54:45 【问题描述】:

我对以下服务数据有一个小问题


    "DataTable": 
        "DataList1": 

            "StudentName": "Rakesh",
            "StudentId": "13",
            "StudentAge": "19",
            "StudentAddress": "NewYork",
        ,
        "DataList2": [
            "TeacherName": "Abhinav",
            "TeacherId": "309",
            "TeacherAge": "34",
            "TeacherAddress": "NewYork",
        ]
    

我可以从 DataList1 中获取数据,但不知道如何从 DataList2 中获取数据。下面是我尝试过的代码。请帮助找出解决方案。提前致谢

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    [jsonArray removeAllObjects];
    NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
    self.responseData = nil;

    NSMutableDictionary *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:@"DataTable"];
    NSMutableArray * myArray = [[NSMutableArray alloc] init];

    if (([(NSString*)sdf isEqual: [NSNull null]])) 
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Information" message:@"Currently there are no Data" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert setTag:1];
        [alert show];
    else 

        [myArray addObject:[sdf objectForKey:@"DataList1"]];

        jsonArray=[myArray mutableCopy];
        refilldict=[jsonArray objectAtIndex:0];
        NSArray *keys = [refilldict allKeys];
        for(int p=0 ; p<[keys count]; p++ )
        
            NSString *value=[refilldict objectForKey:[keys objectAtIndex:p]];
            if ([value isEqual: [NSNull null]]||[value length]==0) 

                [refilldict setValue:@"" forKey:[keys objectAtIndex:p]];

            

        
 lblStudentName.text = [refilldict objectForKey:@"StudentName"];
        lblStudentId.text = [refilldict objectForKey:@"StudentId"];
        lblStudentAge.text = [refilldict objectForKey:@"StudentAge"];

        lblStudentAddress.text = [refilldict objectForKey:@"StudentAddress"];

 self.navigationController.navigationBar.userInteractionEnabled = YES;
    [HUD hide:YES];
    [HUD removeFromSuperview];
    HUD=nil;


【问题讨论】:

你可以通过sdf.allKeys循环从字典键值中提取数据,就像普通数组一样,你目前只能从DataList1获取值 你的代码很乱:if (([(NSString*)sdf isEqual: [NSNull null]])): 为什么要演员?为什么要用双括号? NSMutableDictionary *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:@"DataTable"]; 我不知道-JSONValue 是什么,但你确定sdf 是可变的,即[(NSDictionary*)[responseString JSONValue] objectForKey:@"DataTable"] 是可变的吗?等 【参考方案1】:

请使用下面的代码并在其中传递您的 Initial Json 字典。

-(void)parseJsonData:(NSDictionary *)jsonDictionary

    for(int i=0;i<[jsonDictionary allKeys].count;i++)

        NSString *keyName = [[jsonDictionary allKeys] objectAtIndex:i];
        id objValue = [jsonDictionary objectForKey:keyName];

        if([objValue isKindOfClass:[NSArray class]])

            NSArray *dataList2Array = (NSArray *)objValue;
            NSLog(@"DataList2 Is :--%@",dataList2Array);
        
        else 

            NSDictionary *dataList1 = (NSDictionary *)objValue;
            NSLog(@"DataList1 Is :--%@",dataList1);
        
    

【讨论】:

【参考方案2】:

你可以轻松获得喜欢

[myArray addObject:[sdf objectForKey:@"DataList1"]];

它以字典数组开头,因此您需要将第二个对象存储到数组并从索引中获取。

  NSArray *temp =  [sdf objectForKey:@"DataList2"];
  if(temp.count >0)
   
    lblTeacherName.text = temp[0][@"TeacherName"];
    lblTeacherId.text = temp[0][@"TeacherId"];
    lblTeacherAge.text = temp[0][@"TeacherAge"];
    lblTeacherAddress.text = temp[0][@"TeacherAddress"];
  

【讨论】:

以上是关于从服务获取数据目标 c的主要内容,如果未能解决你的问题,请参考以下文章

无法从目标 C 中的 JSON Web 服务获取值

C# 使用 tcp/ip 从 Minecraft 服务器获取数据包

为啥当我单击目标 c 中的按钮时未从 tableViewCell 获取文本字段数据?

目标 c - 获取数据(核心数据)

有没有办法使用 Parse 服务器获取实时对象 |目标c

从R中给定列名和行名的数据集中获取值