parse json using JsonModel
ios, iphone, jsonmodel
Solution
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.
Problem
Facing problem while getting a array of json. I am using JsonModel library for json handelling. My json data is: ``` [ { "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" } ] ``` Above json has 2 objects of type `TSCard class` ``` //TSCard.h #import <Foundation/Foundation.h> #import <JSONModel.h> @protocol TSCard @end @interface TSCard : JSONModel @property (nonatomic, retain) NSString *TSCard_id; @property (nonatomic, retain) NSString *TSCardBackend_id; @property (nonatomic, retain) NSString *TSCard_date; @property (nonatomic, retain) NSString *TSCard_resource_id; @property (nonatomic, retain) NSString *TSCard_resource_name; @property (nonatomic, retain) NSString *TSCard_job_id; @property (nonatomic, retain) NSString *TSCard_job_desc; @property (nonatomic, retain) NSString *TSCard_activity_id; @property (nonatomic, retain) NSString *TSCard_activity_desc; @property (nonatomic, retain) NSString *TSCard_hours; @property (nonatomic, retain) NSString *TSCard_chargeableflag; @property (nonatomic, retain) NSString *TSCard_desc; @property (nonatomic, retain) NSString *TSCard_syncflag; @property (nonatomic, retain) NSString *TSCard_flag; @property (nonatomic, retain) NSString *user_id; @end ``` And I am making a class for array of type `TSCard` ``` //GetCardData.h #import <JSONModel.h> #import "TSCard.h" @interface GetCardData : JSONModel @property(strong,nonatomic)NSArray<TSCard>* myDataArray; @end ``` then I parsing json as below: ``` NSError* err = nil; GetCardData *c = [[GetCardData alloc] initWithString:jsonString error:&err]; NSLog(@"%d",c.myDataArray.count); ``` NSLog Error: Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'." UserInfo=0x8265490 {NSLocalizedDescription=Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'.} I can't find where I am wrong...can anybody suggest the right way of using `JsonModel` and parsing a jsonString to array of `TSCard` objects correctly.