Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:01:33+00:00 2026-06-12T08:01:33+00:00

I have a an array that I populate that I would in turn like

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:01:35+00:00Added an answer on June 12, 2026 at 8:01 am

    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:

    -(void) connectionDidFinishLoading:(NSURLConnection *)connection {
        self.settingsArray = [NSMutableArray array];
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
        if ([result isKindOfClass:[NSArray class]]) {
    
            for (NSDictionary *item in result) {
                NSString *settingsID = [item objectForKey:@"ID"];
                NSString *category = [item objectForKey:@"Category"];
                NSString *categoryID = [item objectForKey:@"CatalogID"];
                NSString *facings = [item objectForKey:@"Facings"];
                NSString *quantity = [item objectForKey:@"Quantity"];
    
                Setting *setting = [[Setting alloc] initWithName:settingsID desc:category categoryID:categoryID facings:facings quantity:quantity];
                [self.settingsArray addObject:setting];
            }
    
        }
    }
    

    The init method in Setting should look like this:

    - (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;
    
    }
    

    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:

        for (Setting *aSetting in _settingsArray) {
            if ([aSetting.category isEqualToString:@"Frozen"]) {
                NSLog(@"%@",aSetting.facings);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have an array that looks something like this: Array ( [0] => apple
i would like to have an array of objects in excel that call one
I have a javascript array of objects that I would like to use to
I have an array that I populate with parents, children, children of children, children
I have an array that was populated by this script : var imgs =
I have an array that contains @sign in the object. I can't access that
I would like to create an object array in C# of undefined length and
I have an array declared in my header file like this: int snapshot[kSnapshotSize]; which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.