I want to convert the nsmutable array into json array in objective c but I am getting some extra characters also
My code
if (isSucceeded) {
NSMutableDictionary *dictFieldValue = [NSMutableDictionary dictionary];
NSMutableArray *fieldSelectedOptions = [[NSMutableArray alloc]init];
[fieldSelectedOptions addObject:str];
[dictFieldValue setObject:@(fieldData.fieldId) forKey:@"field_id"];
NSError *error;
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:fieldSelectedOptions options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
[dictFieldValue setObject:jsonString forKey:@"f_value"];
[arrFieldValues addObject:dictFieldValue];
NSLog(@"arrFieldValues:%@",arrFieldValues);
}
Output
arrFieldValues:(
{
"field_id" = 128;
"field_value" = 5;
},
{
"f_value" = "[\"1\"]";
"field_id" = 129;
}
)
but i want the output like
arrFieldValues:(
{
"field_id" = 128;
"field_value" = 5;
},
{
"f_value" = ["2"];
"field_id" = 129;
}
)
I have convert the msmutable array into json array and then NSstring and Add that string into an another array in objective. Please let me know my mistake here.
NSJSONWritingPrettyPrinted. It adds unnessary whitespace and newline characters. And what are the string manipulations and regular expression for?NSJSONSerializationcreates the proper JSONstringByReplacingOccurrencesOfStringand related methods is almost certainly going to screw the contents up. What are actually trying to achieve?f_valueis JSONified inside JSON. Use another NSJSONSerialization for it. Also, do not manipulate the JSON as String withstringByReplacingMatchesInString.