【问题标题】:JSON parsing with AFNetworking and model Class使用 AFNetworking 和模型类解析 JSON
【发布时间】:2015-10-05 05:05:34
【问题描述】:

我在使用模型从 json 获取数据并将数据显示到我的应用程序时有点困惑。

我有这种 json 数据:

{
result 
 [
    {
        key : value
        key : value
        key : value
    }
    {
        key : value
        key : value
        key : value
    }
 ]

}

我正在尝试这种代码:

json = [[NSMutableArray alloc]init];
            NSError *writeError = nil;
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&writeError];

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&writeError];


            json = dic[@"Result"];
            int i;
            for (i = 0; i <= json.count; i++)
            {
                NSMutableArray *array = json[i];
                [json enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
                 {
                     // Dim = [[DimEntityFull alloc]initWithJSONDictionary:obj];
                      saveSearch = [[SaveSearchMaster alloc]initWithJSONDictionary:obj];
                 } ];
            }

我正在使用“AFNetworking”,我正在尝试获取数据并存储到模型类中,然后显示到自定义单元格标签。

我怎样才能得到它。

谢谢。

【问题讨论】:

  • 您是否尝试过在提取的每个阶段记录数据?
  • 是的,但它对我不起作用。我对如何存储到模型类有点困惑。
  • 我知道 arrayOfRecords = [jsonData objectForKey:@"result"];然后 cell.lblName.text = [[arrayOfRecords objectAtIndex:indexPath.row]valueForKey:@"name"];是正确的流程。但是使用模型类我怎样才能得到相同的结果?

标签: ios objective-c json afnetworking


【解决方案1】:

在您的视图控制器中

- (void)viewDidLoad
{
    [super viewDidLoad];


    [self getUsersList];
}

-(void)getUsersList
{



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager POST:[NSString stringWithFormat:@"http://www.yourdomainname.com/getUserList"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {

        //we will add Modal class objects of users in this array

        usersArray=[[NSMutableArray alloc]init];

        //getting result dictionary from response
        NSDictionary *resultDictinary = [responseObject objectForKey:@"result"];
        for (NSDictionary *userDictionary in resultDictinary)
        {
            //allocating new user from the dictionary
            User *newUSer=[[User alloc]initWithDictionary:userDictionary];
            [usersArray addObject:newUSer];
        }

        //in users array, you have objects of User class

        //reload your tableview data
        [self.TableView reloadData];


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

}

现在创建名为“用户”的新文件
User.h

@interface User : NSObject
{
    NSString *userID;
    NSString *firstName;
    NSString *lastName;

}
@property (nonatomic, retain)NSString *userID;
@property (nonatomic, retain)NSString *firstName;
@property (nonatomic, retain)NSString *lastName;

-(id)initWithDictionary:(NSDictionary *)sourceDictionary;


@end

User.m
#import "用户.h"

@implementation User
@synthesize userID,firstName,lastName;


-(id)initWithDictionary:(NSDictionary *)sourceDictionary
{
    self = [super init];
    if (self != nil)
    {
        self.firstName=[sourceDictionary valueForKey:@"firstName"];
        self.lastName=[sourceDictionary valueForKey:@"lastName"];
        self.userID=[sourceDictionary valueForKey:@"userID"];
    }
    return self;

}
@end

在您的numberOfRowsInSection方法中
return usersArray.count;

在您的 cellForRowAtIndexPath 方法中

User *user=(User *)[usersArray objectAtIndex:indexPath.row];
yourLabel.text=user.firstName;

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多