parse json using JsonModel

≯℡__Kan透↙ 提交于 2019-12-05 18:36:40
Janak Nirmal

Try using following code for parsing your json and getting array

NSMutableArray *yourArray = [TSCard arrayOfModelsFromDictionaries:jsonString];

Than create and assign

GetCardData *objCardData = [[GetCardData alloc] init];
objCardData.myDataArray = yourArray;

or you need to change your JSONString as follow

   {
    "myDataArray": [
        {
            "TSCard_id": 100,
            "TSCardBackend_id": "TSu1t1",
            "TSCard_date": "10/11/2013",
            "TSCard_resource_id": "",
            "TSCard_resource_name": "",
            "TSCard_job_id": "JOB_M2156 ",
            "TSCard_job_desc": "BAGARIA MILLIPORE - BOM ",
            "TSCard_activity_id": "B03",
            "TSCard_activity_desc": "Closing",
            "TSCard_hours": "4.0",
            "TSCard_chargeableflag": "0",
            "TSCard_desc": "Description:",
            "TSCard_syncflag": "0",
            "TSCard_flag": "",
            "user_id": "1"
        },
        {
            "TSCard_id": 101,
            "TSCardBackend_id": "TSu1t2",
            "TSCard_date": "12/11/2013",
            "TSCard_resource_id": "",
            "TSCard_resource_name": "",
            "TSCard_job_id": "JOB_B0002",
            "TSCard_job_desc": "ABB LIMITED - BLR ",
            "TSCard_activity_id": "NB01",
            "TSCard_activity_desc": "Admin  ",
            "TSCard_hours": "5.0",
            "TSCard_chargeableflag": "1",
            "TSCard_desc": "Description:",
            "TSCard_syncflag": "0",
            "TSCard_flag": "",
            "user_id": "1"
        }
    ]
}

Than your code will definitely work. Tell me if need more help.

For your problem, I personally recommend BWJSONMatcher. You don't have to do anything else or declare any protocol apart from your data model:

@interface TSCard : NSObject

@property (nonatomic, strong) NSString  *TSCard_id;
@property (nonatomic, strong) NSString  *TSCardBackend_id;

@property (nonatomic, strong) NSString  *TSCard_date;
@property (nonatomic, strong) NSString  *TSCard_resource_id;
@property (nonatomic, strong) NSString  *TSCard_resource_name;
@property (nonatomic, strong) NSString  *TSCard_job_id;
@property (nonatomic, strong) NSString  *TSCard_job_desc;
@property (nonatomic, strong) NSString  *TSCard_activity_id;
@property (nonatomic, strong) NSString  *TSCard_activity_desc;
@property (nonatomic, strong) NSString  *TSCard_hours;
@property (nonatomic, strong) NSString  *TSCard_chargeableflag;
@property (nonatomic, strong) NSString  *TSCard_desc;
@property (nonatomic, strong) NSString  *TSCard_syncflag;
@property (nonatomic, strong) NSString  *TSCard_flag;
@property (nonatomic, strong) NSString  *user_id;

@end

You can then get your array with one neatly short line:

NSArray *dataArray = [BWJSONMatcher matchJSON:jsonString withClass:[TSCard class]];

You can using this method in JSONModel

NSError *error;
NSMutableArray <TSCard *> *yourArray = [TSCard arrayOfModelsFromString:strData error:&error];

and do your logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!