将NSDictionary从json结果保存到NSUserDefaults会返回“尝试插入非属性列表对象”错误

时间:2014-06-02 08:10:53

标签: ios objective-c

你好我有这样的json数据

来自NSLog的结果(@" JSON sec blnce list:%@",responseObject);:

{
detail =     (
            {
        account = 9X000101300160;
        balance = "1.000.000";
        date = "<null>";
        price = 0;
        secCode = 9999;
        type = Blocked;
        value = 0;
    },
            {
        account = 9X000101300160;
        balance = "1.000.000";
        date = "<null>";
        price = 0;
        secCode = 9999;
        type = Available;
        value = 0;
    }
);
header =     {
    date = 24092012;
    investorID = IDD010239865303;
    securityType = Equity;
};
}

我想要做的是将此json结果保存到NSUserDefaults中。这是我如何保存: 在.h

@property NSDictionary *jsonResult;

in .m

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        [manager POST:URI_SECURITY_BALANCE_DETAIL parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {

            [SVProgressHUD dismiss];


            NSLog(@"JSON sec blnce list: %@", responseObject);

            self.jsonResult = responseObject;

            //simpan hasil query ke device
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:self.jsonResult forKey:@"securityBalanceNextJsonResult"];
            [defaults synchronize];

但是上面的代码给了我一个错误:&#34;尝试设置一个非属性列表对象&#34;

我如何保存从json结果到NSUserDefaults的NSDictionary?

更新:(解决) 可能是有人有同样的问题,这是我的工作代码:

self.jsonResult = responseObject;

        NSData *data = [NSJSONSerialization dataWithJSONObject:self.jsonResult options:kNilOptions error:nil];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"securitiesBalanceJsonResult"];

        NSError* error;
        NSDictionary* jsonDB = [NSJSONSerialization
                              JSONObjectWithData:[defaults objectForKey:@"securitiesBalanceJsonResult"]

                              options:kNilOptions 
                              error:&error];

        NSLog(@"data:%@",jsonDB);

1 个答案:

答案 0 :(得分:6)

我能想到的唯一解释是你的服务器在JSON中发送空值。当结果转换为NSDictionary时,NSNull的实例将插入到字典中以表示这些空值。

但是,NSNull不是属性值,这就是您无法将字典存储在用户默认值上的原因。正如NSUserDefaults documentation所述:

  

NSUserDefaults类提供了便于访问的方法   常见类型,例如浮点数,双精度数,整数,布尔值和URL。一个   默认对象必须是属性列表,即(或   对于集合的实例组合):NSData,NSString,   NSNumber,NSDate,NSArray或NSDictionary。如果你想存储任何   在其他类型的对象中,通常应将其存档以创建   NSData的实例。

您有两种选择:

  1. 删除带空值的键(在示例中,date中的detail)。
  2. 将字典另存为NSData
  3. 第一个:

    NSMutableDictionary *mutable = [dictionary mutableCopy];
    NSArray *array = mutable[@"detail"];
    NSMutableArray *mut = [NSMutableArray arrayWithCapacity:[array count]];
    for (NSDictionary *d in array)
    {
        NSMutableDictionary *m = [NSMutableDictionary dictionary];
        for (NSString *k in d) {
            id v = d[k];
            if (![v isKindOfClass:[NSNull class]]) {
                m[k] = v;
            }
        }
        [mut addObject:m];
    }
    mutable[@"detail"] = mut;
    

    第二个:

    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:nil];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];