I have a an array that I populate that I would in turn like to place in a strongly-typed object. I have this code that has been populated with json data and I would like to populate my Setting object with this data. How can I do that?
JSON response:
Got response as:
[
{
"CatalogID":2377,
"Category":"Frozen",
"Facings":true,
"ID":13,
"LastUpdateDateTime":"\/Date(1348681365520-0700)\/",
"Quantity":true
},
{
"CatalogID":2377,
"Category":"Fruit",
"Facings":true,
"ID":10,
"LastUpdateDateTime":"\/Date(1348692069843-0700)\/",
"Quantity":false
},
{
"CatalogID":2377,
"Category":"Salads",
"Facings":true,
"ID":12,
"LastUpdateDateTime":"\/Date(1348681354807-0700)\/",
"Quantity":true
},
{
"CatalogID":2377,
"Category":"Vegetables",
"Facings":true,
"ID":11,
"LastUpdateDateTime":"\/Date(1348681334523-0700)\/",
"Quantity":true
}
]
Objective-C
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *error = nil;
// Get the JSON data from the website
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
NSStringEncoding responseEncoding = NSUTF8StringEncoding;
if ([response textEncodingName]) {
CFStringEncoding cfStringEncoding = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)[response textEncodingName]);
if (cfStringEncoding != kCFStringEncodingInvalidId) {
responseEncoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding);
}
}
if(receivedData)
{
NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:responseEncoding];
NSLog(@"Got response as %@", dataString);
}
if ([result isKindOfClass:[NSArray class]]) {
for (NSArray *item in result) {
NSArray *items = [item valueForKey:@"ID"];
[_settings addObject:items];
// Try to populate Setting object
Setting *setting = (Setting *)[_settings objectAtIndex:0];
NSLog(@"Setting: %@", setting);
}
}
else {
NSDictionary *jsonDictionary = (NSDictionary *)result;
for(NSDictionary *item in jsonDictionary)
NSLog(@"Item: %@", item);
}
NSLog(@"Finished");
}
Setting.h
@interface Setting : NSObject {
NSString *_settingsID;
NSString *_catalogID;
NSString *_category;
NSString *_facings;
NSString *_quantity;
}
@property (nonatomic, retain) NSString *settingsID;
@property (nonatomic, retain) NSString *catalogID;
@property (nonatomic, retain) NSString *category;
@property (nonatomic, retain) NSString *facings;
@property (nonatomic, retain) NSString *quanity;
- (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category;
@end
Setting.m
@implementation Setting
@synthesize settingsID = _settingsID;
@synthesize catalogID = _catalogID;
@synthesize category = _category;
@synthesize facings = _facings;
@synthesize quanity = _quantity;
- (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category CategoryID:(NSString *)catalogID Facings:(NSString *)facings Quantity:(NSString *)quantity {
if ((self = [super init])) {
self.settingsID = settingsID;
self.catalogID = catalogID;
self.category = category;
self.facings = facings;
self.quanity = quantity;
}
return self;
}
@end
There are quite a few things wrong in the code you posted. I will run through them and show you how to do it correctly, but I would highly suggest that you study the docs for NSArray, NSDictionary, and NSString to familiarize yourself with the methods available to you in those classes — you really can’t even begin to write decent code without knowing those classes well.
1) I don’t really know what you’re trying to do with the string encoding stuff and the code after if(receivedData). There’s no need to convert your JSON into a string, the JSONObjectWithData: method converts your data to an array (more precisely, an array of dictionaries), and that’s what you need to work with.
2) In your for-in loop, item should be typed as an NSDictionary, not an array. Each item in the array is the whole dictionary between the {} in your JSON that you posted.
3) All the code you have in the for-in loop is wrong — I can’t tell what you’re trying to do with that. What you need to do is get the values for the various keys in your dictionary, alloc init an instance of Setting, and pass the values in that extended init method.
4) Your setting object just keeps the values for one of the dictionaries in your JSON data, so you need to create another array to hold one setting object for each dictionary in your response (I called it settingsArray in my example below).
5) Your init method in the Setting class is different in the .h and .m, they should both look like the .m version, except that the pieces of the name after each colon should be lower case not capitalized.
So, here is the code that I think will work. Since I don’t have access to your JSON, I can’t test it, so try it out and get back to me with the results.
So, in the class where you are doing your download, you should have a property, settingsArray, and the following code in the connectionDidFinishLoading method:
The init method in Setting should look like this:
After Edit: If you want to find the value of one of the properties of a setting based on the value of another property, you can do it like this: