JSON 错误 - JSON 写入中的类型无效 (Posm)

Posted

技术标签:

【中文标题】JSON 错误 - JSON 写入中的类型无效 (Posm)【英文标题】:JSON Error - Invalid type in JSON write (Posm) 【发布时间】:2013-11-29 10:01:40 【问题描述】:

我在实体类上有一个数组(带有 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序列化兼容?

谢谢。

【问题讨论】:

你能贴出方法的代码` [Util getPosmArrayPreference:(NSstring*)str];` 吗? 感谢 rpy.. 我已经更新了我的问题。 我得到了我的 array1 的值。 您的数组包含哪种对象? 我在控制台向你展示..(lldb) po array1 $0 = 0x1e19d540 <__nsarraym>( ,,,) 【参考方案1】:

看起来您正在将 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] ;

【讨论】:

@kaar3k我已经使用 [dictionnary setObject:array1 forKey:@"First"]; 将其转换为字典 但关键@“First”,我没有在应用程序的其他任何地方使用。对吗? @kaar3kHere 我已经使用了你的代码..但是由于未捕获的异常“NSInvalidArgumentException”,它在同一个地方崩溃并出现以下错误终止应用程序,原因:“JSON 写入中的类型无效(NSConcreteMutableData)跨度> 你改行了吗NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; [dictionnary setObject:posmJSONArray forKey:@"First"]; 加入聊天会话chat.***.com/rooms/info/42188/nsjsonserialization

以上是关于JSON 错误 - JSON 写入中的类型无效 (Posm)的主要内容,如果未能解决你的问题,请参考以下文章

JSON 写入错误中的类型无效,尝试通过 JSON 将自定义类发送到 .NET Web 服务

JSONSerialization JSON 写入中的类型无效 (_SwiftValue)

JSONSerialization JSON 写入中的类型无效 (_SwiftValue)

Swift - JSON 写入中的***类型无效

在 Swift 中写入 JSON 中的***类型无效

尝试使用 JSONModel 序列化后出错:JSON 写入中的类型无效 (...)