美文网首页
JSONString

JSONString

作者: 张家杰仔 | 来源:发表于2017-04-06 10:43 被阅读16次
    - (NSString *)jsonStringFromObject:(id)object { //OC对象转JSON
        NSError *erro = nil;
        NSData *jsondata = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&erro];
        NSString *jsonString = [[NSString alloc]initWithData:jsondata encoding:NSUTF8StringEncoding];
        return jsonString;
    }
    
    
    //把jsonString转换成任意对象
    - (id)jsonStringToObject:(NSString *)jsonString
    {
        NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error;
        id object = [NSJSONSerialization JSONObjectWithData:data
                     
                                                    options:NSJSONReadingAllowFragments
                     
                                                      error:&error];
        if(error) {
            
            NSLog(@"json解析失败:%@",error);
            return nil;
        }
        return object;
    }
    

    相关文章

      网友评论

          本文标题:JSONString

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