1

I need convert json string from this array

( {
        1 = 4;
    },
        {
        8 = 20;
    })

Actually i need output like {"custom":{"1":"3","8":"21"}} but i got "{\n \"custom\" : \"[\\n {\\n \\\"1\\\" : \\\"4\\\"\\n },\\n {\\n \\\"8\\\" : \\\"20\\\"\\n }\\n]\"\n}"

here is my code

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mydict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

is there any way to get output like this : {"custom":{"1":"3","8":"21"}}

0

2 Answers 2

3

Just replace NSJSONWritingPrettyPrinted with 0.

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

2 Comments

Thank you now i got [{"1":"4"},{"8":"20"}] . can you help with how to make like this {"7":"8","8":"5"}
@Subramani - you have an array of dictionaries, if you want to output a single JSON dictionary the produce a single dictionary from your array before doing the JSON conversion.
0

try removing the NSJSONWritingPrettyPrinted option like this :

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mydict options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

According to the documentation NSJSONWritingPrettyPrintedadds whitespace to make the output readable , removing that option gives the most compact JSON possible . More here

1 Comment

Thank you and i already done this is there any way to make like this {"7":"8","8":"5"} from [{"1":"4"},{"8":"20"}]

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.