i use JSON but there is exception [NSCFString JSONValue]

Discussion in 'iOS Programming' started by ITCreative, Dec 16, 2011.

  1. ITCreative macrumors member

    Joined:
    Aug 4, 2011
    #1
    i read information in my database i use JSON but there is exception


    I think exception in this part

    Code:
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
       NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];
    
    
    NSArray *array = [str JSONValue];
    if (!array)
        return;
    
    NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateFormat:@"yyyy-MM-dd"];
    
    for (NSDictionary *dict in array) {
    
    NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
        NSLog(@"%@",d);
        [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];
    
    
    
    }
    
    
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    have u tried running your JSON through www.jsonlint.com ? so you know if there is an error or not.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    You are sending a JSONValue message to str which is a NSString instance. Does NSString respond to this message?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    It can... if you remember to import the header of the JSON library you are using.
     
  5. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #5
    ya i import it but i didn't now how solve this exception :(
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  7. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #7
    in this http://code.google.com/p/json-framework/

    ----------

    I try put NSLog to know which line is exception

    when put NSlog before this line
    Code:
    	NSArray *array = [str JSONValue];
    
    
    it is printed

    after it doesn't printed :(

    ----------

    Additional info :


    I use kal source to represent event and read events from database server



    and use JSON with kal source for calendar
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    So there is a category on NSObject. A are you including the .m as well as .h in your application? If not there is no compiled version of the category at runtime which would give exceptions like you are seeing.
     
  9. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #9
    I see the declaration in "NSObject+SBJson.h"

    Make sure you have "SBJson.h" imported.

    Also that all the library source files copied into your project.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Assuming you have #import "SBJson.h" as you claimed, are you sure you copied the library files into your project and not just linked them?

    You should be sure you've followed the instructions under the Installation section of this page, especially Step 3:
    https://github.com/stig/json-framework
     
  11. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #11
    ya i copy all file JSON

    i try print str after

    Code:
    
    	NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];
    	NSLog(str);
    	
    
    

    print all contain in str

    after that is crash !!!!
     
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    The runtime error indicates you may not have. Can you post a screenshot of your Project setup showing the .m files, specifically them getting compiled into your application executable?
     
  13. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #13

    in interface :

    Code:
    #import <Foundation/Foundation.h>
    #import "Kal.h"
    
    @class Events;
    @interface EventsSqliteDataSource : NSObject <KalDataSource>
    
    {
    	NSMutableArray *items;
    	NSMutableData *buffer;
    	NSMutableArray *eventPHP;
    	id<KalDataSourceCallbacks> callback;
    	BOOL dataReady;
    }
    @property (nonatomic, retain) NSMutableArray *eventPHP;
    
    + (EventsSqliteDataSource *)dataSource;
    - (Events *)EventsAtIndexPath:(NSIndexPath *)indexPath; 
    
    @end
    



    in implementation

    Code:
    
    #import "JSON.h"
    #import "EventsSqliteDataSource.h"
    #import "Events.h"
    
    static BOOL IsDateBetweenInclusive(NSDate *date, NSDate *begin, NSDate *end)
    {
    	return [date compare:begin] != NSOrderedAscending && [date compare:end] != NSOrderedDescending;
    }
    
    @interface EventsSqliteDataSource ()
    - (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate;
    - (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate;
    @end
    
    
    @implementation EventsSqliteDataSource
    @synthesize eventPHP;
    
    
    + (EventsSqliteDataSource *)dataSource
    {
    	return [[[[self class] alloc] init] autorelease];
    }
    
    - (id)init
    {
    	if ((self = [super init])) {
    		items = [[NSMutableArray alloc] init];
    		eventPHP = [[NSMutableArray alloc] init];
    		buffer = [[NSMutableData alloc] init];
    	}
    	return self;
    }
    
    - (Events *)EventsAtIndexPath:(NSIndexPath *)indexPath
    {
    	return [items objectAtIndex:indexPath.row];
    }
    
    #pragma mark UITableViewDataSource protocol conformance
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    	static NSString *identifier = @"MyCell";
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    	if (!cell) {
    		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    		cell.selectionStyle = UITableViewCellSelectionStyleNone;
    		//cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    	}
    	
    	Events *event = [self EventsAtIndexPath:indexPath];
    	//cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"flags/%@.gif", holiday.country]];
    	
    	//NSDictionary *dict = [eventPHP objectAtIndex: indexPath.row];
    	
    	//cell.textLabel.text = [dict objectForKey:@"title_event"];
    	cell.textLabel.text = event.title;
    	return cell;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    	return [items count];
    }
    
    #pragma mark Fetch from the internet
    
    - (void)fetchEvents
    {
    	NSString *path = @"http://www.iksu-app.com/event.php";
    	NSLog(@"Fetching %@", path);
    	dataReady = NO;
    	[eventPHP removeAllObjects];
    	NSURLConnection *conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] delegate:self];
    	[conn start];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    	[buffer setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    	[buffer appendData:data];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    	NSString *str = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
    
    	NSArray *array = [str JSONValue];
    
    	if (!array)
    		return;
    	
    	NSLog(@"after this line crash");
    
    	NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    	[fmt setDateFormat:@"yyyy-MM-dd"];
    	
    	for (NSDictionary *dict in array) {
    		NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
    	
    		NSLog(@"%@",d);
    		[eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"] description:[dict objectForKey:@"description"] date:d]];
    		
    		
    		
    	}
    	
    	
    	
    	
    	dataReady = YES;
    	[callback loadedDataSource:self];
    }
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    	NSLog(@"EventsCalendarDataSource connection failure: %@", error);
    }
    
    #pragma mark KalDataSource protocol conformance
    
    - (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
    {
    	if (dataReady) {
    		[callback loadedDataSource:self];
    		return;
    	}
    	
    	callback = delegate;
    	[self fetchEvents];
    }
    
    - (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
    {
    	if (!dataReady)
    		return [NSArray array];
    	
    	return [[self eventsFrom:fromDate to:toDate] valueForKeyPath:@"eve_date"];
    }
    
    - (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
    {
    	if (!dataReady)
    		return;
    	
    	[items addObjectsFromArray:[self eventsFrom:fromDate to:toDate]];
    }
    
    - (void)removeAllItems
    {
    	[items removeAllObjects];
    }
    
    #pragma mark -
    
    - (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate
    {
    	NSMutableArray *matches = [NSMutableArray array];
    	for (Events *event in eventPHP)
    		if (IsDateBetweenInclusive(event.date, fromDate, toDate))
    			[matches addObject:event];
    	
    	return matches;
    }
    
    - (void)dealloc
    {
    	[items release];
    	[eventPHP release];
    	[buffer release];
    	[super dealloc];
    }
    
    
    
    @end
    
    
    

    [​IMG]


    :(
     
  14. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    None of that is what I have asked for: are the .m files that form the actual implementation of the various headers included by JSON.h actually in your project. You are seeing an error that indicates they are not. You are including the headers so the NSObject category is available at compile time. But the error is very clear: it's not available at run time. So the most likely outcome is you have not included the .m files in the project. Once again: please provide a screenshot showing that you have included the .m files in your project and that they are being compiled into the final executable.
     
  15. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
  16. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #16
    And are those actually being compiled into the executable?
     
  17. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #17
    may be in this function ya, because my project contain other function and use many resource and class for it.

    but in this function just i trace every steps in example Holiday hear :

    https://github.com/klazuka/Kal

    it use JSON
     
  18. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #18
    Is should my file which read from data server "event.json" ?

    or not problem with "event.php" ?

    because the example holiday which trace it the end of url ".json"
     
  19. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #19
    I solve exception by change location files class of JSON but the event doesn't

    display in calendar .

    the calendr is display but didn't contain any event :confused:

    hint: when i put url in browser the content of database display
     
  20. akshay.jain89 macrumors newbie

    Joined:
    Jun 11, 2012
    #20
    yes, i have faced this problem too. This code will work in score 4.2 , 3.2 etc But the latest Xcode Give you the NSString Error on the Line where you used JSONValue. Idont know why this error occurs in Xcode 4.3.2. But this code will run on Xcode 4.2. check it using Xcode 4.2 or 3.2
     

Share This Page