0

I need some kind of json string generator for objective-c. Actually I thought there must be something like that but I could not find anything.To be specific, for example I have a json string like:

{"name":"abc","email":"[email protected]","password":"1"}

when I want to store it in objective c, I have to write it like:

@"{\"name\":""\"abc\""",\"email\":""\"[email protected]\""",\"password\":""\"1\"""}"

so it is confusing and hard to implement. Are there any generators or an easy way to implement it. Thanks

1
  • 3
    You have a JSON string like that where? In a file?? What do you mean by "store it in objective c"? Commented Apr 18, 2013 at 10:08

4 Answers 4

1

Convert your json string to dictionary...

NSData* data = [yourJsonString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary* jsonDict = [NSJSONSerialization
                                      JSONObjectWithData:data
                                      options:kNilOptions
                                      error:&error];
 NSLog(@"jsonDict:%@",jsonDict);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the native ios SDK API's for generating JSON data from NSDictionary / NSArray.

e.g.,

NSData *jsondata = [NSJSONSerialization dataWithJSONObject:(dictobject) options:NSJSONWritingPrettyPrinted error:&error];

NSString *str = [[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];

may this helps!

Comments

0

Use it Also Import"JSON.H"

 NSDictionary *googleResponse = [[NSString stringWithContentsOfURL:[NSURL  URLWithString:website] encoding: NSUTF8StringEncoding error: NULL] JSONValue];

Comments

0

be careful...in your original question, when referring to how to store it in objective-c, i think you made a mistake

you wrote : @"{\"name\":""\"abc\"""

when it shoud be @"{\"name\":\"abc\",....etc...

i think you doubled the quotes...be wary, cause in the beginning there already is one opening double-quote @", all the other needed quotes must be escaped like so \", and then also have an ending quote as well.

hope it helps

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.