How to convert a Json String into a NSArray?
Solution 1
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *values = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // if you are expecting the JSON string to be in form of array else use NSDictionary instead
The above code works good for iOS 5 and above
Solution 2
Try importing RestKit/RKJSONParserJSONKit.h. Then use try the method objectFromString:error:. To get a NSDictionary representation. From the NSDictionary you can get the array.
Sebastian Boldt
I am a web and mobile software architect and developer specializing in iOS. Passionate about creating awesome user experiences, designing beautiful user interfaces, and writing maintainable, structured, and best-practice orientated code. Continuously trying to improve skills and learn new technologies.
Updated on June 13, 2022Comments
-
Sebastian Boldt almost 2 years
I am currently trying to convert the JSON Representation of some Objects into an NSArray. I used RestKit to get the Response through our API and now I want to convert the RKResponse into an Array of Objects. How can I do this ?
-
Sebastian Boldt over 11 yearsOk maybe i should describe my problem a little bit more detailed. I got an Array of Json Objects in form of a RKRESPONSE and now i want to convert it into an Array with real Objective -C Objects with propertys and so one ...
-
AppleDelegate over 11 yearsWhat is the dataType of RKRESPONSE.Is it NSDATA or some other??
-
Sebastian Boldt over 11 yearsRKRESPONSE is defined inside the RestKit Headers. It has its own Type.
-
Sebastian Boldt over 11 yearsI can convert it to a string like the following : NSData *tempWishes = [[response bodyAsString]dataUsingEncoding:NSUTF8StringEncoding];
-
AppleDelegate over 11 yearsso just use bypass the first line of my answer and subject tempWishes instead of 'data' for JSONSerialization.The output should be an array
-
AppleDelegate over 11 yearsis it an array of NSDictionaries??
-
ashokdy over 10 yearsThank it worked for me and saved my time, so one up
-
Jonathan F. almost 10 yearsWorks fine, and pretty simple.