删除重复的JSON数据对象IOS7

时间:2014-03-20 07:55:35

标签: ios json post

您好,在我的应用程序中,我使用sqlite3 post方法将JSON数据库数据传递到我的在线服务器,因为当我打印JSON数据时,每条记录都会给出两个重复记录示例我有名称xxx的记录表示JSON显示三次的同一记录请告诉我如何删除重复记录。

 [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field1Str] forKey:@"name"];
        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field2Str] forKey:@"phone"];
        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field3Str] forKey:@"city"];
        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",field4Str] forKey:@"email"];

        [array addObject:_dataDictionary];


        NSData* jsonData = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:nil];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        NSURL *someURLSetBefore =[NSURL URLWithString:@"url"];
        [request setURL:someURLSetBefore];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setHTTPBody:jsonData];

        // print json:
        NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
                                                         encoding:NSUTF8StringEncoding]);
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];

如果打印我已将数据存储到数据字典中的数组,那么它正确显示但在json中仅显示重复记录,请告诉我如何解决此问题。

感谢。

1 个答案:

答案 0 :(得分:0)

更改选项参数

options:kNilOptions

options:NSJSONWritingPrettyPrinted

在这一行

 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:nil];