写给自己的网络请求解析 返回数组和字典类型不同的处理方法

一.数组类型

返回类型是个数组的这个比较常见

{
    "result": {
        "total": 3,
        "rows": [
            {
                "ati_id": 5,
                "ati_title": "zzzxxxcccc",
                "atc_name": "Birthday Party",
                "ci_nikename": "yangchi",
                "ati_datetime": "十月 28, 2015",
                "ati_start_time": "18:00",
                "ati_end_time": "21:00",
                "ati_people_count": 20,
                "ati_city": "杭州",
                "ati_address": "杭州滨江",
                "ati_info": "玩耍",
                "ati_date": "2015-10-27 13:55:45",
                "picture_small_network_url": "uploadFiles/2015/10/26/20151026163435_1526a95f751-0708-4598-b0a2-a90c3332e448_small.jpg"
            },
            {
                "ati_id": 4,
                "ati_title": "gogogo",
                "atc_name": "Birthday Party",
                "ci_nikename": "yangchi",
                "ati_datetime": "十月 27, 2015",
                "ati_start_time": "18:00",
                "ati_end_time": "21:00",
                "ati_city": "杭州",
                "ati_address": "杭州滨江",
                "ati_info": "玩耍",
                "ati_date": "2015-10-26 11:44:26"
            },
            {
                "ati_id": 1,
                "cr_remark_name": "第二",
                "ati_title": "生日聚会",
                "atc_name": "Birthday Party",
                "ci_nikename": "Lucas Rondez",
                "ati_datetime": "十月 27, 2015",
                "ati_start_time": "18:00",
                "ati_end_time": "21:00",
                "ati_people_count": 10,
                "ati_city": "杭州",
                "ati_address": "杭州滨江",
                "ati_info": "hello",
                "ati_date": "2015-10-26 10:13:30"
            }
        ]
    },
    "code": "0",
    "message": "success"
}

就像这种的你需要取到字典里的‘“rows”

方法如下:

写网络请求

//传入参数

NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"ci_id"] = [AppConfigure valueForKey:LOGINED_USER_ID];

//发送请求

 [HttpManager requestActivityList:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

responseObject//返回的数据是个字典

  //获得数据
        NSArray *activities = [Activity objectArrayWithKeyValuesArray:[[responseObject objectForKey:@"result"] objectForKey:@"rows"]];//activitties是个数组因为返回值类型是个数组

Activity 是个类 就是我们俗称的model类


 Activity *activity =  _partActivities[indexPath.row];
    cell.activity = activity;



之后就在自定义的cell里面把数据传过去就行了



二.返回的是个字典类型

这种一般用不到indexpath 一般用来显示具体信息的

for Example

{
    "result": {
        "ati_id": 1,
        "cr_remark_name": "第二",
        "ati_title": "生日聚会",
        "atc_name": "Birthday Party",
        "ci_nikename": "Lucas Rondez",
        "ati_datetime": "十月 27, 2015",
        "ati_start_time": "18:00",
        "ati_end_time": "21:00",
        "number_of_participants": 2,
        "ati_people_count": 10,
        "ati_city": "杭州",
        "ati_address": "杭州滨江",
        "ati_info": "hello",
        "ati_date": "2015-10-26 10:13:30",
        "pictures": []
    },
    "code": "0",
    "message": "success"
}


同样的先写请求

//请求的参数

 NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"ci_id"] = [AppConfigure valueForKey:LOGINED_USER_ID];
    params[@"ati_id"] = @(self.acitivityID);

//发送请求

[HttpManager requestActivityDetails:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);

_dataDic = responseObject[@"result"];//取到result 因为result是个字典所以要创建个字典去接收


self.eventDetail = [Detail objectWithKeyValues:_dataDic];//eventDetail是model类 Detail的对象   因为要取到这个字典里面的东西 需要用一个对象去接收


self.eventDetail这个就是最后的对象了 里面有你定义的所有属性值 之后赋值就是了


你可能感兴趣的:(ios,json)