2018-06-05 jSON使用

封包

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];

// isValidJSONObject判断对象是否可以构建成json对象

if ([NSJSONSerialization isValidJSONObject:dic]){

NSError *error;

    // 创造一个json从Data, NSJSONWritingPrettyPrinted指定的JSON数据产的空白,使输出更具可读性。

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicoptions:NSJSONWritingPrettyPrinted error:&error];

    NSString *json =[[NSString alloc]initWithData:jsonDataencoding:NSUTF8StringEncoding];

    NSLog(@"json data:%@",json);

你可能感兴趣的:(2018-06-05 jSON使用)