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.
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.
Related
Well, I should remember you I am still a beginner in iOS development with objective c. And maybe because of this, the solution is simple but I can´t see it. I have a json who is armed in the following way :
{
"origin_addresses": [
"Test"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "0.3 km",
"value": 339
},
"duration": {
"text": "1 min",
"value": 82
},
"status": "OK"
}
]
}
],
"status": "OK"
}
The app can consume the service without problems. My goal is to reach the distance attribute. Which I do as follows. I use a foreach to get through the array rows and then another foreach to get through the array elements.
for (BPCGARows *item in googleAddress.rows) {
NSLog(#"rows : %#", item);
for (BPCGAElements *element in item.elements) {
}
}
When I'm debugging the app, my first for each works without problems, but when I access the second foreach the (BPCGAElements * element in item.elements) the app crashes. Finally I can't print the item.elements in a log either. The error message is as follows:
'NSInvalidArgumentException', reason: '-[__NSSingleEntryDictionaryI
elements]: unrecognized selector sent to instance 0x281c75da0'
header BPCGADistance :
#interface BPCGADistance : MBOInspectableModel
#property (nonatomic, strong, readonly) NSString *text;
#property (nonatomic, strong, readonly) NSNumber *value;
#end
main BPCGADistance:
#interface BPCGADistance()
#property (nonatomic, strong, setter=text:) NSString *text;
#property (nonatomic, strong, setter=value:) NSNumber *value;
#end
header BPCGAElements :
#interface BPCGADistance : MBOInspectableModel
#property (nonatomic, strong, readonly) NSString *text;
#property (nonatomic, strong, readonly) NSNumber *value;
#end
main BPCGAElements:
#interface BPCGADistance()
#property (nonatomic, strong, setter=text:) NSString *text;
#property (nonatomic, strong, setter=value:) NSNumber *value;
#end
This is my NSLog(#"rows : %#", item),
I emphasize that by using the item.elements to print the crashea app on the console. ;
I have a master realm object:
#interface MasterRealmObject : RLMObject
#property (nonatomic, strong) RLMArray<IDNameRealmObject *><IDNameRealmObject> *retailerType;
#property (nonatomic, strong) RLMArray<IDNameRealmObject *><IDNameRealmObject> *firmType;
#property (nonatomic, strong) RLMArray<IDNameRealmObject *><IDNameRealmObject> *businessAge;
#property (nonatomic, strong) RLMArray<StateRealmObject *><StateRealmObject> *state;
#property (nonatomic, strong) RLMArray<KYCDocsRealmObject *><KYCDocsRealmObject> *kycDocs;
#property (nonatomic, strong) RLMArray<ProofRealmObject *><ProofRealmObject> *businessDocs;
#property (nonatomic, strong) NSString *ReligareTollFreeNumber;
#end
I'm trying to store values from a Dictionary as below:
-(void)insertMasterAPIObjects:(NSDictionary *)masterDictionary
{
RLMRealm *realmInsertMasterObjects = [RLMRealm defaultRealm];
[realmInsertMasterObjects beginWriteTransaction];
MasterRealmObject *masterRealm = [[MasterRealmObject alloc]init];
masterRealm.retailerType = [masterDictionary objectForKey:#"retailer_type"];
masterRealm.firmType = [masterDictionary objectForKey:#"firm_type"];
masterRealm.businessAge = [masterDictionary objectForKey:#"BusinessAge"];
masterRealm.kycDocs = [masterDictionary objectForKey:#"kyc_docs"];
masterRealm.businessDocs = [masterDictionary objectForKey:#"business_docs"];
masterRealm.state = [masterDictionary objectForKey:#"states"];
[realmInsertMasterObjects addObject:masterRealm];
[realmInsertMasterObjects commitWriteTransaction];
}
I'm getting this exception here on the first line in this method.
I hit a similar problem because I had omitted the
RLM_ARRAY_TYPE(IDNameRealmObject)
RLM_ARRAY_TYPE(StateRealmObject)
RLM_ARRAY_TYPE(KYCDocsRealmObject)
RLM_ARRAY_TYPE(ProofRealmObject)
macros before my #interface declaration.
I have object
#interface Hourly (CoreDataProperties)
#property (nullable, nonatomic, retain) NSDate *dt;
#property (nullable, nonatomic, retain) NSNumber *temp;
#property (nullable, nonatomic, retain) NSNumber *temp_min;
#property (nullable, nonatomic, retain) NSNumber *temp_max;
#property (nullable, nonatomic, retain) NSNumber *humidity;
#property (nullable, nonatomic, retain) NSNumber *pressure;
#property (nullable, nonatomic, retain) NSString *main;
#property (nullable, nonatomic, retain) NSString *des;
#property (nullable, nonatomic, retain) NSString *icon;
#end
And url
http://api.openweathermap.org/data/2.5/forecast/dayly?lat=35&lon=139&cnt=2
I can' add json to object by jsonmodel pod because weather in list is a array .
#Ngọc Thái:
I don't really understand your question. But, if you want parse from JSON (contain array of objects) to object.
Simple, you can try it: https://github.com/elado/jastor#arrays
Hope this help!
Try my project BWJSONMatcher. It helps you easily convert a json string to NSArray:
NSArray *dataArray = [BWJSONMatcher matchJSON:YOURJSONSTRING withClass:[Hourly class]];
The library is fully opensourced and under MIT license.
I need some help configuring RestKit 0.2x... I am returning JSON with a Schedule object and ScheduleEntries as a nested array.
Sample JSON:
[{
"id": 3,
"name": "Schedule 9",
"sWeek": 9,
"sYear": 2014,
"companyId": 1,
"createdOn": "2014-02-11T22:01:33.547",
"modifiedOn": null,
"companyCode": "0001",
"isActive": true,
"notes": "This is a test note",
"scheduleEntries": [{
"id": 15,
"companyId": 1,
"userId": "a7e46520-8db7-4452-821d-7938b23fcd07",
"scheduleId": 3,
"jobId": 5,
"isActive": true,
"createdOn": "2014-02-11T22:01:33.993",
"modifiedOn": null,
"sWeek": 11,
"sYear": 2014,
"startTime": "2014-03-10T08:48:00",
"endTime": "2014-03-10T08:48:00"
}, {
"id": 16,
"companyId": 1,
"userId": "a7e46520-8db7-4452-821d-7938b23fcd07",
"scheduleId": 3,
"jobId": 3,
"isActive": true,
"createdOn": "2014-02-11T22:01:34.003",
"modifiedOn": null,
"sWeek": 11,
"sYear": 2014,
"startTime": "2014-03-11T00:57:00",
"endTime": "2014-03-11T00:57:00"
}, {....
NSManagedObjects
#interface Schedule : NSManagedObject
#property (nonatomic, retain) NSNumber * scheduleId;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * sWeek;
#property (nonatomic, retain) NSNumber * sYear;
#property (nonatomic, retain) NSNumber * companyId;
#property (nonatomic, retain) NSString * companyCode;
#property (nonatomic, retain) NSString * notes;
#property (nonatomic, retain) NSNumber * isActive;
#property (nonatomic, retain) NSDate * createdOn;
#property (nonatomic, retain) NSDate * modifiedOn;
#property (nonatomic, retain) NSSet * scheduleEntries;
#end
#implementation Schedule
#dynamic scheduleId;
#dynamic name;
#dynamic sWeek;
#dynamic sYear;
#dynamic companyId;
#dynamic companyCode;
#dynamic notes;
#dynamic isActive;
#dynamic createdOn;
#dynamic modifiedOn;
#dynamic scheduleEntries;
#end
#interface ScheduleEntry : NSManagedObject
#property (nonatomic, retain) NSNumber * scheduleEntryId;
#property (nonatomic, retain) NSNumber * companyId;
#property (nonatomic, retain) NSNumber * usrId;
#property (nonatomic, retain) NSNumber * scheduleId;
#property (nonatomic, retain) NSNumber * jobId;
#property (nonatomic, retain) NSNumber * sWeek;
#property (nonatomic, retain) NSNumber * sYear;
#property (nonatomic, retain) NSDate * startTime;
#property (nonatomic, retain) NSDate * endTime;
#property (nonatomic, retain) NSNumber * isActive;
#property (nonatomic, retain) NSDate * createdOn;
#property (nonatomic, retain) NSDate * modifiedOn;
#end
#implementation ScheduleEntry
#dynamic scheduleEntryId;
#dynamic companyId;
#dynamic usrId;
#dynamic scheduleId;
#dynamic jobId;
#dynamic sWeek;
#dynamic sYear;
#dynamic startTime;
#dynamic endTime;
#dynamic isActive;
#dynamic createdOn;
#dynamic modifiedOn;
#end
Code:
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
// Create mapping for entity
RKEntityMapping *scheduleEntryMapping = [RKEntityMapping mappingForEntityForName:#"ScheduleEntry" inManagedObjectStore:managedObjectStore];
scheduleEntryMapping.identificationAttributes = #[#"scheduleEntryId"];
[scheduleEntryMapping addAttributeMappingsFromDictionary:#{
#"id" : #"scheduleEntryId",
#"companyId": #"companyId",
#"usrId" : #"usrId",
#"scheduleId" : #"scheduleId",
#"jobId" : #"jobId",
#"isActive": #"isActive",
#"sWeek": #"sWeek",
#"sYear": #"sYear",
#"startTime": #"startTime",
#"endTime": #"endTime",
#"createdOn": #"createdOn",
#"modifiedOn": #"modifiedOn"
}];
RKEntityMapping *scheduleMapping = [RKEntityMapping mappingForEntityForName:#"Schedule" inManagedObjectStore:managedObjectStore];
scheduleMapping.identificationAttributes = #[#"scheduleId"];
[scheduleMapping addAttributeMappingsFromDictionary:#{
#"id" : #"scheduleId",
#"name": #"name",
#"sWeek": #"sWeek",
#"sYear": #"sYear",
#"companyId": #"companyId",
#"companyCode": #"companyCode",
#"notes": #"notes",
#"isActive": #"isActive",
#"createdOn": #"createdOn",
#"modifiedOn": #"modifiedOn",
#"scheduleEntries": #"scheduleEntries"
}];
[scheduleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"scheduleEntries"
toKeyPath:#"scheduleEntries"
withMapping:scheduleEntryMapping]];
This code is very close to the RestKit 0.2x example but I can't seem to get it to work. I can get a single object just fine, but when it comes to objects that have nested data I'm stuck. It has been a few days working on this with no results. The error says I'm setting the keyPath more than once but I don't see it. Any help would be appreciated.
You just need to remove #"scheduleEntries": #"scheduleEntries" from the mapping dictionary on scheduleMapping because that is added (and therefore duplicated) by the relationship mapping.
Other than that things look correct.
I have been struggling with this issue for some time now and I cant figure out why the JSON serialization is not working. What seems to be a simple task it taking way too long.
I have my model classes listed below. User object has NSArray of Education and Employment objects.
I am using RestKit and all of the relationships have been made and I can see that the dictionary of the parameters is being made the data is correct. In order to debug the issue further I am making JSON using the following code.
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:self.user requestDescriptor:delegate.postUserRequestDescriptor error:&error];
// Serialize the object to JSON
BOOL isTurnableToJSON = [NSJSONSerialization
isValidJSONObject: parameters];
if(isTurnableToJSON){
NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
}
isTurnableToJSON is always "NO". When I force creation of JSON I get the following error.
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Education)'
The same error is also thrown for employment if I remove the relationship of Education with user.
Here is my user, education, and location models.
#interface User : NSObject
#property (strong, nonatomic) NSString *first_name;
#property (strong, nonatomic) NSString *middle_name;
#property (strong, nonatomic) NSString *last_name;
#property (strong, nonatomic) NSString *profile_picture;
#property (strong, nonatomic) NSString *display_name;
#property (nonatomic, strong) NSArray *education;
#property (nonatomic, strong) NSArray *employment;
#property (nonatomic, strong) NSMutableArray *skills;
#property (strong, nonatomic) NSString *about_you;
#end
#interface Education : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSString *graduation_date;
#property (nonatomic, strong) NSString *major;
#property (nonatomic, strong) NSString *degree;
#property (nonatomic, strong) Location *location;
#property (nonatomic) NSNumber *current;
#end
#interface Location : NSObject
#property (nonatomic, strong) NSString *city;
#property (nonatomic, strong) NSString *state;
#property (nonatomic, strong) NSString *country;
#end
At this point I am at lose of any ideas and any help would be appreciated.
Update: RestKit Relationship mapping.
RKObjectMapping *locationMap = [RKObjectMapping mappingForClass:(Location.class)];
[locationMap addAttributeMappingsFromDictionary:#{#"city":#"city",#"state":#"state",#"country":#"country"}];
RKObjectMapping *educationMap = [RKObjectMapping mappingForClass:(Education.class)];
[educationMap addAttributeMappingsFromDictionary:# { #"name":#"name",#"graduation_date":#"graduation_date",#"major":#"major", #"degree":#"degree",#"current":#"current"}];
[educationMap addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"location" toKeyPath:#"location" withMapping:locationMap]];
RKObjectMapping *employmentMap =[RKObjectMapping mappingForClass:(Employment.class)];
[employmentMap addAttributeMappingsFromDictionary:#{#"company":#"company",#"occupation":#"occupation", #"start_date":#"start_date",#"end_date":#"end_date",#"current":#"current"}];
RKObjectMapping *userPutRequestMapping = [RKObjectMapping requestMapping];
[userPutRequestMapping addAttributeMappingsFromDictionary:putUserMap];
[userPutRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"education" toKeyPath:#"education" withMapping:[educationMap inverseMapping]]];
[userPutRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"employment" toKeyPath:#"employment" withMapping:[employmentMap inverseMapping]]];
USER PUT REQUEST
[objectManager addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:userPutRequestMapping
objectClass:[User class]
rootKeyPath:nil
method:RKRequestMethodPUT]];