Anand Gautam Anand Gautam - 5 months ago 16
JSON Question

JSON Error - Invalid type in JSON write (Posm)

I have one array on entity class (with Posm name). So i need to send that entity data to server through php url using NSJSONSerialization. But here after dataWithJSONObject, it is crashing with given error...

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Invalid type in JSON write (Posm)'


here is my code what i did..

NSMutableArray *array1=[[NSMutableArray alloc]init];
array1= [Util getPosmArrayPreference:@"NbPosm"]; //this array1 have Posm entity data, that i need to send using POST http method.

NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:array1 forKey:@"First"];

NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
options:kNilOptions
error:&error];

NSString *urlString =@"http://..........checklist_api.php";

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request setHTTPBody:jsonData];
NSURLResponse *response = NULL;
NSError *requestError = NULL;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;


Posm.h class :-

#import <Foundation/Foundation.h>

@interface Posm : NSObject

@property (nonatomic, retain) NSString *posmId;
@property (nonatomic, retain) NSString *posmName;
@property (nonatomic, retain) NSString *posmQuantity;
@property (nonatomic, retain) NSString *posmRemarks;
@property (nonatomic, retain) NSString *posmAfterImageId;
@property (nonatomic, retain) NSString *posmBeforeImageId;
@property (nonatomic, retain) NSData *posmAfterData;
@property (nonatomic, retain) NSData *posmBeforeData;

@end


Posm.m :-

#import "Posm.h"

@implementation Posm

@synthesize posmId, posmName, posmQuantity, posmRemarks, posmAfterData, posmBeforeData, posmAfterImageId, posmBeforeImageId;

- (id) initWithCoder: (NSCoder *)coder
{
self = [[Posm alloc] init];
if (self != nil)
{
self.posmId = [coder decodeObjectForKey:@"posmId"];
self.posmName = [coder decodeObjectForKey:@"posmName"];
self.posmQuantity = [coder decodeObjectForKey:@"posmQuantity"];
self.posmRemarks = [coder decodeObjectForKey:@"posmRemarks"];

self.posmAfterImageId = [coder decodeObjectForKey:@"posmAfterImageId"];
self.posmBeforeImageId = [coder decodeObjectForKey:@"posmBeforeImageId"];

self.posmAfterData=[coder decodeObjectForKey:@"posmAfterData"];
self.posmBeforeData=[coder decodeObjectForKey:@"posmBeforeData"];
}
return self;
}

- (void)encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:posmId forKey:@"posmId"];
[coder encodeObject:posmName forKey:@"posmName"];
[coder encodeObject:posmQuantity forKey:@"posmQuantity"];
[coder encodeObject:posmRemarks forKey:@"posmRemarks"];

[coder encodeObject:posmAfterImageId forKey:@"posmAfterImageId"];
[coder encodeObject:posmBeforeImageId forKey:@"posmBeforeImageId"];

[coder encodeObject:posmAfterData forKey:@"posmAfterData"];
[coder encodeObject:posmBeforeData forKey:@"posmBeforeData"];
}

@end


+(NSMutableArray *)getPosmArrayPreference:(NSString *)string
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject1 = [defaults objectForKey:string];
NSMutableArray *arr1=[[NSMutableArray alloc]init];
NSMutableArray *arr=(NSMutableArray *) [NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject1];
for (int i=0;i<arr.count;i++) {
NSString *st=[NSString stringWithFormat:@"%d",i];
[arr1 addObject:[Util getPosmPreference:st]];
}
return arr;
}


Am I creating the JSON object wrong? Please suggest me that how can i make the entity class (Posm) JSON serialization compatible?

Thanks.

Answer

Looks like you are adding POSM object into the array and trying to serialise.Convert your POSM object values into dictionary and add the dictionary into array.

Edit find the below updated code

NSMutableArray *array1=[[NSMutableArray alloc]init];
array1= [Util getPosmArrayPreference:@"NbPosm"];  
NSMutableArray *posmJSONArray=[NSMutableArray array];
for (Posm *posm in array1) {
    NSMutableDictionary *posmJSON=[NSMutableDictionary dictionary];
    [posmJSON setValue:posm.posmId forKey:@"posmId"];
    [posmJSON setValue:posm.posmName forKey:@"posmName"];
    [posmJSONArray addObject:posmJSON];
}    
    NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
    [dictionnary setObject:posmJSONArray forKey:@"First"];

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                       options:kNilOptions
                                                         error:&error];

    NSString *urlString =@"http://ebiz.pmgasia.com.sg/iweb/Rms/Mobile/Api/checklist_api.php";

    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:jsonData];
    NSURLResponse *response = NULL;
    NSError *requestError = NULL;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;