美文网首页
字符串与NSData和JSON互转

字符串与NSData和JSON互转

作者: starfox寒流 | 来源:发表于2016-10-27 17:19 被阅读1004次

    NSString * str = @"[ { "product_id": "228", "order_id": "475", "score": "", "images": "", "remark": "sdlakfjalskjf", "lang": "zh_cn", "member_id": "1", "order_number": "810201610271410406835", "item_id": "0" } ]";
    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSArray * result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"字符串转到data,再转成json%@",result);

    NSArray * a = @[
    @{
    @"images" : @"",
    @"item_id" : @0,
    @"lang" : @"zh_cn",
    @"member_id" : @1,
    @"order_id" : @475,
    @"order_number" : @"810201610271017264145",
    @"product_id" : @1151,
    @"remark" : @"第一个商品",
    @"score" : @""
    },
    @{
    @"images" : @"",
    @"item_id" : @0,
    @"lang" : @"zh_cn",
    @"member_id" : @1,
    @"order_id" : @473,
    @"order_number" : @"810201610271017264145",
    @"product_id" : @1152,
    @"remark" : @"第二个商品",
    @"score" : @""
    }
    ];
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:a options:NSJSONWritingPrettyPrinted error:nil];
    NSString * strdd = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"json转成data,再转成字符串%@",result);

    相关文章

      网友评论

          本文标题:字符串与NSData和JSON互转

          本文链接:https://www.haomeiwen.com/subject/waksuttx.html