ios 把数组对象转成json字符串存起来

ios 把数组对象转成json字符串存起来

1第一步是我们获取数据源

一般我们都是从接口请求数据

 NSArray *subColumnsArray = nil;

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers  error:nil];

//保存到数据源,用数组接收

 if ([dict isKindOfClass:[NSDictionary class]]){

        subColumnsArray = [dict objectForKey:@"subColumns"];

    }

 

2第二部就是存一个json字符串到数据源中

if(subColumnsArray != nil){

      //把数组对象传承json字符串存起来

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:subColumnsArray options:NSJSONWritingPrettyPrinted error:nil];

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

       //初始化个字典copy所有对象那个 

        NSDictionary *copydic = [[mutableArray lastObject] mutableCopy];

       //修改字段赋值  然后保存到数据库用title字段来保存

           [copydic setValue:subColumns forKey:@"title"];

        //插入到第几个位置

    }

 

3,接着这是你需要数据源的地方取值,取值需要在给他转化如果存的时候是数组,你转化成熟组,如果存的是字典就转换成字典

    //转换成字典,然后用数组接收  然后cell赋值,把数据源去setmodel

       NSData *jsonData = [article.title dataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData

                                                            options:NSJSONReadingMutableContainers

                                                              error:nil];

        NSArray *arr = [Column mj_objectArrayWithKeyValuesArray:dic];

        cell.imagesModels = arr;

posted @ 2019-03-11 09:59 whx060900 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值