iOS自带Json解析器 转成Json格式的字符串


这个就是转换的方法:

    NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:@"i can fly",@"title",@"4012",@"length",@"Tom",@"Singer", nil];
     NSArray* cityArr = @[song,@"大连",@"辽宁",@"黑龙江",@"上海",@"江苏",@"宁波",@"杭州",@"浙江",@"厦门",@"福建",@"江西",@"山东",@"青岛",@"湖南",@"湖北",@"河南"];
    if ([NSJSONSerialization isValidJSONObject:cityArr])
    {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:cityArr options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"json data:%@",json);
    }


你可能感兴趣的:(iOS)