【问题标题】:From object to json [duplicate]从对象到 json [重复]
【发布时间】:2015-09-03 07:37:33
【问题描述】:

如何转换 JSON 中的对象列表?我搜索了互联网,但我使用的所有方法都出错了。我需要将对象 coll 变成字符串 json

Collections *current = _raccolte[_currentCell];
current.label = [[alertView textFieldAtIndex:0] text];
current.datetime_last_update = [NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]];

NSMutableArray *coll = [[NSMutableArray alloc] init];
[coll addObject:current];
HttpClient *client = [[HttpClient alloc] init];
[client syncCollection:coll];

【问题讨论】:

    标签: ios objective-c json


    【解决方案1】:

    您使用NSJSONSerialization 类将 JSON 转换为 Foundation 对象并将 Foundation 对象转换为 JSON。

    你可以在这里查看Link

    Collections *current = _raccolte[_currentCell];
    current.label = [[alertView textFieldAtIndex:0] text];
    current.datetime_last_update = 
    [NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]];
    
    NSMutableArray *coll = [[NSMutableArray alloc] init];
    [coll addObject:current.label];
    [coll addObject:current.datetime_last_update];
    
    NSError *writeError = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:coll 
                                                       options:NSJSONWritingPrettyPrinted 
                                                         error:&writeError];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData 
                                                 encoding:NSUTF8StringEncoding]; 
    
    NSLog(@"JSON Output: %@", jsonString);
    

    【讨论】:

    • 代替 NSArray* coll = [NSArray arrayWithObjects:current.label,current.datetime_last_update];我必须传递一个对象列表 NSMutableArray *coll
    • 您也可以使用NSMutableArray @FlavioMerolli 获取我使用过的信息Array。检查我编辑的答案
    • 使用NSArray *coll = [NSArray arrayWithObjects:collection];。集合是 NSMutableArray 我得到线程 1:EXC_BAD_ACCESS 在方法调度中缺少哨兵
    • 检查更新的答案@FlavioMerolli
    • 在 NSMutableArray *coll 我有一个列表或项目集合。我必须得到一个 jsonObject 的列表
    【解决方案2】:

    关于json序列化的问题很多:iOS JSON serialization for NSObject-based classes

    主要思想是将您的对象转换为NSDictionary,然后将其转换为 jsonData 并(如果需要)转换为字符串。

    在您的情况下,您需要创建NSDictionariesNSArray,然后将其传递给如下方法:

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array 
                                                       options:NSJSONWritingPrettyPrinted 
                                                         error:&writeError];
    

    如果你需要字符串,你需要做类似的东西:

    NSString* jsonString = [[NSString alloc] initWithData:jsonData 
                                                 encoding:NSUTF8StringEncoding];
    

    【讨论】:

      【解决方案3】:

      首先,您必须确保您的集合的对象是可序列化的。您可以通过使您的Collections 对象符合NSCoding 协议来实现此目的。这需要实现两个基本方法:(instancetype)initWithCoder:(NSCoder *)aDecodervoid)encodeWithCoder:(NSCoder *)aCoder

      那么你可以简单的使用下面的handler静态方法

      - (NSString*)convertObjectToJson:(NSObject *)object {
          NSError *error = nil;
      
          NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object 
                                                             options:NSJSONWritingPrettyPrinted 
                                                               error:&error];
          NSString *result = [[NSString alloc] initWithData:jsonData 
                                                   encoding:NSUTF8StringEncoding];
          return result;
      }
      

      这适用于您自己的Collections 实例和NSMutableArray,默认情况下符合NSCoding

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-15
        • 1970-01-01
        相关资源
        最近更新 更多