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 8024471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:52:49+00:00 2026-06-04T22:52:49+00:00

I am trying to pass data to labels from my JSON file onto a

  • 0

I am trying to pass data to labels from my JSON file onto a simple ViewController but I don’t know where to actually pass that data. Would I be able to just add to my setDataToJson method or would I add the data in my viewDidLoad method?

here is my code

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation;
@end

@implementation NSDictionary(JSONCategories)

+(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{
    NSData* data = [NSData dataWithContentsOfFile:fileLocation];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data 
                                                options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}
@end

@implementation ViewController
@synthesize name;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)setDataToJson{

    NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"];
    name.text = [infomation objectForKey:@"AnimalName"];//does not pass data
}
  • 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-04T22:52:50+00:00Added an answer on June 4, 2026 at 10:52 pm

    The problem is the way you’re trying to retrieve your file. In order to do it right, you should find first its path in the bundle. Try something like this:

    +(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{
        NSString *filePath = [[NSBundle mainBundle] pathForResource:[fileLocation stringByDeletingPathExtension] ofType:[fileLocation pathExtension]];
        NSData* data = [NSData dataWithContentsOfFile:filePath];
        __autoreleasing NSError* error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data 
                                                    options:kNilOptions error:&error];
        // Be careful here. You add this as a category to NSDictionary
        // but you get an id back, which means that result
        // might be an NSArray as well!
        if (error != nil) return nil;
        return result;
    }
    

    After doing that and once your view is loaded, you should be able to set your labels by retrieving the json like this:

    -(void)setDataToJson{
        NSDictionary *infomation = [NSDictionary dictionaryWithContentsOfJSONString:@"Test.json"];
        self.name.text = [infomation objectForKey:@"AnimalName"];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self setDataToJson];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass post data from one page to the next but PreviousPage
I'm trying to pass data that is saved in sessionStorage from background.html to popup.html
I am trying to make an app that will pass data between two servers
I'm trying to make simple script using jquery $post function to pass data to
I'm trying to write my own protocol so that multiple servers can pass data
I am trying to pass data from the ViewBag object to javascript on my
I am trying to pass data from SQL, to C#, then to an R
I'm trying to pass data from one activity to another via Intent.putExtras like this:
I am trying to pass data allocated in the heap through g_signal_connect but it
I've been struggling to make it work, but cannot pass the inserted data from

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.