9

Imagine I make an request to my api that return an mysql_query("Select * from user where id=1").

After the request is done, it returns the user info in json. I do some debuging, NSlog(@"JSON : %@",json); and it gives me this:

JSON : (
    {
    aboutme = "";
    active = 0;
    birthday = "1992-10-14";
    "city_id" = 0;
    email = "[email protected]";
    fbid = "";
    firstname = test;
    gender = 1;
    id = 162;
    lastname = test;
    password = "$2a$12$8iy.sGr.4V/Ea3GfHZe0m.SLDrvoSj3/wYRlWsNce1yyCMeCbDrMC";
    "phone_number" = "";
    "recovery_date" = "0000-00-00 00:00:00";
    "register_date" = "2013-06-06 02:44:20";
    salt = "8iy.sGr.4V/Ea3GfHZe0m";
    "user_type_id" = 1;
    username = test;
}
)

Now I parse it with AFJONDecode and when I get the [json valueForKey:@"username"]; and debug it ( NSlog(@"username = %@",[json valueForKey@"username"]); ) and I get this:

username = (
    testeteste739
)

It gives me an object ( because in the Json, the username = test and not username = "test").

So, how can i convert this object to string?

** UPDATE **

I resolve it by the following way:

NSArray *username = [JSON valueForKey:@"username"];
username = [username objectAtIndex:0];

Is there any better way to overpass this? Thanks

9
  • 2
    The value of username is an NSString with a value of @"test". Just because it doesn't log with quotes doesn't mean it isn't a string. Commented Jun 6, 2013 at 1:57
  • I don't think so, because if i do NSlog(@"username = --%@--", username) i get: username = --( testeteste1009 )-- Commented Jun 6, 2013 at 2:04
  • 2
    First off, what you have above is not a JSON string. It is a dump of NSDictionary and NSArray objects, representing the interalized form of the JSON. In other words, it is already "parsed". Commented Jun 6, 2013 at 2:14
  • What you have there is an NSArray (apparently named "json") which contains a single array element which is an NSDictionary. To get, eg, username you'd first extract element zero of json (NSDictionary* dict = [json objectAtIndex:0]) and then extract the appropriate array element (NSString* username = [dict objectForKey:@"username"];). Commented Jun 6, 2013 at 2:18
  • 2
    And note that since the above is an Objective-C object dump, and not JSON, string values that have no blanks or special characters are shown without enclosing quotes. Commented Jun 6, 2013 at 2:20

1 Answer 1

27

As JSON is a dictionary object, so you can get your json data to JSONDic NSDictionary variable and parse it to string as follows:-

NSDictionary *JSONDic=[[NSDictionary alloc] init];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:JSONDic
                                                   options:NSJSONWritingPrettyPrinted 
                                                     error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Sign up to request clarification or add additional context in comments.

4 Comments

That is, of course, the way to create a JSON string, given an internal representation in NSArray/NSDictionary objects. (Hopefully your JSONDict object has contents when you actually do this.) I think the OP is still struggling to get values OUT of the JSON.
As question title specifies the he want to convert JSON to string I did this , he didn't specify in the title that he want values out of JSON.
He doesn't know what he wants.
All i wanted was to get the value "username". I already resolve it, so thanks everyone and sorry for all my confusion!

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.