我在实体类上有一个数组(带有Posm名称)。所以我需要使用NSJSONSerialization通过php url将该实体数据发送到服务器。但是在dataWithJSONObject之后,它会因给定的错误而崩溃......
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Invalid type in JSON write (Posm)'
这是我的代码我做了什么..
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课程: -
#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;
}
我是否创建了错误的JSON对象?请建议我如何使实体类(Posm)JSON序列化兼容?
感谢。
答案 0 :(得分:0)
看起来您正在将POSM对象添加到数组中并尝试序列化。将POSM对象值转换为字典并将字典添加到数组中。
编辑查找以下更新的代码
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] ;