How to convert a Json String into a NSArray?

12,731

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.

Share:
12,731
Sebastian Boldt
Author by

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, 2022

Comments

  • Sebastian Boldt
    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
    Sebastian Boldt over 11 years
    Ok 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
    AppleDelegate over 11 years
    What is the dataType of RKRESPONSE.Is it NSDATA or some other??
  • Sebastian Boldt
    Sebastian Boldt over 11 years
    RKRESPONSE is defined inside the RestKit Headers. It has its own Type.
  • Sebastian Boldt
    Sebastian Boldt over 11 years
    I can convert it to a string like the following : NSData *tempWishes = [[response bodyAsString]dataUsingEncoding:NSUTF8StringEncoding];
  • AppleDelegate
    AppleDelegate over 11 years
    so just use bypass the first line of my answer and subject tempWishes instead of 'data' for JSONSerialization.The output should be an array
  • AppleDelegate
    AppleDelegate over 11 years
    is it an array of NSDictionaries??
  • ashokdy
    ashokdy over 10 years
    Thank it worked for me and saved my time, so one up
  • Jonathan F.
    Jonathan F. almost 10 years
    Works fine, and pretty simple.