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
usernameis anNSStringwith a value of@"test". Just because it doesn't log with quotes doesn't mean it isn't a string.username = --( testeteste1009 )--NSDictionary* dict = [json objectAtIndex:0]) and then extract the appropriate array element (NSString* username = [dict objectForKey:@"username"];).