0

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.

6
  • Forget NSJSONWritingPrettyPrinted. It adds unnessary whitespace and newline characters. And what are the string manipulations and regular expression for? NSJSONSerialization creates the proper JSON Commented Jan 2, 2018 at 12:27
  • Thanks @vadian , then what i want to use there? Commented Jan 2, 2018 at 12:29
  • Manipulating JSON with stringByReplacingOccurrencesOfString and related methods is almost certainly going to screw the contents up. What are actually trying to achieve? Commented Jan 2, 2018 at 12:30
  • Please stop randomly printing the contents of the arrFieldValues, that output is meaningless. What do you plan on doing with it? Commented Jan 2, 2018 at 12:31
  • f_value is JSONified inside JSON. Use another NSJSONSerialization for it. Also, do not manipulate the JSON as String with stringByReplacingMatchesInString. Commented Jan 2, 2018 at 12:35

1 Answer 1

1

Remove NSJSONWritingPrettyPrinted and all string manipulation code.

Replace

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:fieldSelectedOptions options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
jsonString =[jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"[,\\.\']" options:0 error:NULL];

jsonString = [expression stringByReplacingMatchesInString:jsonString options:0 range:NSMakeRange(0, jsonString.length)
                                                                         withTemplate:@""];
jsonString =[jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];
[dictFieldValue setObject:jsonString forKey:@"f_value"];

with

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:fieldSelectedOptions options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
dictFieldValue[@"f_value"] = jsonString;

I'm wondering why almost all tutorials suggest the pretty printed option.

Servers don't care about aesthetics at all.

Sign up to request clarification or add additional context in comments.

5 Comments

I guess tutorials uses the pretty print option for teaching reasons: It is easier to check and explain the result.
Vadian ,please check my updated code..now also i am getting some extra characters
The extra characters are pretty fine. The backslashes are added virtually to escape the double quotes in the literal string description.
i don't want any extra characters .i want like this arrFieldValues:( { "field_id" = 128; "field_value" = 5; }, { "f_value" = ["2"]; "field_id" = 129; } )
* sigh * . Once again, there are no extra characters. They are added virtually when calling the description method to be able to display double quotes in a literal string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.