1

Probaly a duplicate but can't seem to find a solution. Is there a way in objective-c to convert:

{
    "a" : "value1",
    "b" : 1,
    "c" : "value3",
    "d" : "value4",
}

to:

"{\r\n\t\"a\" : \"value1\",\r\n\t\"b\" : 1,\r\n\t\"c\" : \"value3\",\r\n\t\"d\" : \"value4\",\r\n}"

Thank you

1
  • What do you mean by converting JSON object? There is really no such thing. JSON is a STRING format. It is not clear what the input is here is it a JSON string in pretty print and you are trying to collapse it to JSON string that is on single line with whitespace characters preserved? Why do you care about whitespace (as this is actually not valid part JSON specs)? Really the two strings you show are equivalent. Commented Mar 31, 2015 at 21:23

1 Answer 1

1

I used the following code to convert a JSON object to a NSString in JSON format.

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:yourDictionary options:kNilOptions error:nil];
NSString *strJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Hope this helps you.

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

Comments

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.