0

I wave read many articles about parsing a json via an Http request but almost none answers the question about what is the most straight forward way to parse o local json string. I found some deferent solutions here Deserializing local NSString of JSON into objects via RestKit (no network download) some of them work some others don't. Is there any "official" RestKit support for local JSON string deserialization?

Community
  • 1
  • 1

2 Answers2

3

Fortunately Apple provides "NSJSONSerialization" class from iOS 5.0. to serialise your data. Step 1 : convert your local JSON string to "NSData"

     NSString *strLocalJSON = @"{data= "some data"}"; //just for ex. may not be valid one
     NSData *dataJSON = [str dataUsingEncoding:NSUTF8StringEncoding];

Step2:

   id jsonobject= [NSJSONSerialization JSONObjectWithData:dataJSON options:NSJSONReadingMutableContainers error:nil]; // resulting object may contain Dictionary or Array depends on your localjson

for further info refer https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

Akshay
  • 227
  • 2
  • 13
0

If your JSON is in a file then you use a file:// NSURL when you create your RestKit object manager. Everything else works as normal.

If the JSON is already in code, then you should save it to a file, or why are you needing to do any mapping? Anyway, you could use a combination of NSJSONSerialization and RKMappingOperation.

Wain
  • 118,658
  • 15
  • 128
  • 151