0

I am trying to parse NSArray to JSON but I get the following error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0xa93e460' * First throw call stack: (0x21f1012 0x1feae7e 0x227c4bd 0x21e0bbc 0x21e094e 0x3445a 0x33ecc 0x26a453f 0x26b6014 0x26a72e8 0x26a7450 0x95e22e12 0x95e0acca) libc++abi.dylib: terminate called throwing an exception

I have included all classes from SBJson_3.1.1/Classes directory.
This is code:

NSMutableArray* arr = ...get array
NSString* jsonArr = [arr JSONRepresentation]; // here I get error

When I do this in array of simple strings it works:

 NSData *jsonData = [NSJSONSerialization arr
                                                       options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

But my array contain list of objects (Person) maybe there is a problem.

I use Item instead of person just as example
Item.h

@interface Item : NSObject
{
    BOOL IsOpen;
    NSString* Description;
}

@property int ItemId;
@property int SequenceId;
@property BOOL IsOpen;
@property NSString* Description;

- (id) proxyForJson;

@end

Item.m

@implementation Item
@synthesize ItemId;
@synthesize SequenceId;
@synthesize Description;
@synthesize IsOpen;

- (id) proxyForJson {

    return [NSDictionary dictionaryWithObjectsAndKeys:
            [NSString stringWithFormat:@"%i", ItemId], @"ItemId",
            SequenceId, @"SequenceId",
            Description, @"Description",
            IsScanned, @"IsOpen",
            nil ];
}
@end



UPDATE

Student example
I tried to make a separate project. I copied to new project all from classes directory of sbjson framework. This is code:

#import "SBJson.h" 

@interface Student : NSObject
{
    NSString *name;
    NSInteger sid;

    NSString *email;
}
@property NSString *name;
@property NSInteger sid;

@property NSString *email;

- (id) proxyForJson;

@end

@implementation Student
@synthesize name;
@synthesize sid;

@synthesize email;

- (id) proxyForJson{
    return [NSDictionary dictionaryWithObjectsAndKeys:
            name, @"student_name",
            [NSNumber numberWithInt:sid], @"student_id",
            email, @"email",
            nil ];
}

@end

NSMutableArray* studentArray = [[NSMutableArray alloc]init];

    Student* s1 = [[Student alloc]init];
    s1.name = @"student 1";
    s1.sid = 45;
    s1.email = @"test@test.com";

    Student* s2 = [[Student alloc]init];
    s2.name = @"student 2";
    s2.sid = 46;
    s2.email = @"plavi@test.com";

    [studentArray addObject:s1];
    [studentArray addObject:s2];

    NSString *jsonString = [studentArray JSONRepresentation];

    NSLog(@"%@", jsonString);

And again I get error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0x741b100'

1110
  • 7,829
  • 55
  • 176
  • 334
  • 1
    Please see several similar questions: http://stackoverflow.com/questions/9420126/sbjson-parsing-nsstring-to-nsdictionary & http://stackoverflow.com/questions/11937587/ios-objective-c-json-string-to-nsdictionary-exception & This comment: http://stackoverflow.com/a/5214540/5950 – Stig Brautaset Nov 01 '12 at 14:24
  • 1
    I google last night but nothing helps including those stack questions ;( and I have just updated my question with more information about issue. – 1110 Nov 02 '12 at 07:31
  • You're missing the protocol .m file on NSArray that defines `JSONRepresentation`. – Hot Licks Nov 03 '12 at 18:13
  • Your second example which works shows that you are using NSJSONSerialization. Why dont you use that? What was the issue you are facing with that. You could have tried with that and updated question. – iDev Nov 12 '12 at 06:13
  • It seems that the custom object scenario not working is a red herring. Are you saying that trying to use `JSONRepresentation` on an array of `NSString` values also gives you the same error? Because that's really the problem. If everything else is working properly, SBJSON is going to give the message **-JSONRepresentation failed. Error is: JSON serialisation not supported for ** if your custom object is not supported. Your error is due to source/library reference issues. – KevinH Nov 12 '12 at 06:50
  • 1
    And incidentally, I set up a working example with a basic custom object, a `proxyForJson` implementation, and copying the latest set of classes from the [SBJSON GitHub repository](https://github.com/stig/json-framework/) directly into my project. The issue is not your code implementation. – KevinH Nov 12 '12 at 07:02

4 Answers4

0

SBJson doesn't support serialising user-defined classes without assistance. If you implement a -proxyForJson method in your Person class (example here) it should work, however.

If you're using a recent Xcode the below should work. Header:

@interface Item : NSObject
@property int ItemId;
@property int SequenceId;
@property BOOL IsOpen;
@property(copy) NSString* Description;
- (id) proxyForJson;
@end

Implementation:

@implementation Item
- (id) proxyForJson {
    return @{ @"ItemId": @(self.ItemId),
              @"SequenceId": @(self.SequenceId),
              @"Description": self.Description,
              @"IsOpen": @(self.IsOpen)
              };
}
@end

This should let SBJson serialise the Item objects to NSDictionaries. However, SBJson does not support parsing JSON into custom objects. So you will always get this back in the dictionary form. I don't know of any Objective-C JSON parser that provides bindings to custom types.

Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39
  • I have updated my question and set bounty can you check please? – 1110 Nov 05 '12 at 07:44
  • I still get error:( : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0xf73c5c0' – 1110 Nov 05 '12 at 08:43
0

I would suggest reading the top two comments of this thread. If those don't help, it is still very likely that you are not installing the library correctly. Try removing the SBJSON files from your project and then readding them, making sure that they are added to your target. Also, make sure you are importing the SBJSON header into your class.

I would suggest that you try using JSONRepresentation on an array of NSString objects. If the framework is correctly installed, this should definitely work. This way you can narrow down whether it is an installation issue or whether it is an issue with your custom class.

Community
  • 1
  • 1
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
0

Check out the following excerpt from Working with JSON in iOS 5 Tutorial This is mainly for generating JSON.

//build an info object and convert to json
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
[loan objectForKey:@"name"], 
@"who",
[(NSDictionary*)[loan objectForKey:@"location"] 
objectForKey:@"country"], 
@"where",
[NSNumber numberWithFloat: outstandingAmount], 
@"what",nil];

//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];

Now, the difference lies in using NSDictionary and converting that into JSON Data. Try forming the JSON in the way given above and check if the problem persists.

0

you are correctly linking the category? to me it kinda looks like you are missing a category

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135