0

How can I convert general JSON string into Objective-C formatted JSON.

Here is the example:

{
  "data": {
    "resultGenerationTimestamp": 1460803271,
    "deleted": [],
    }
}

I am getting this response from Postman. I need to make it JSON and add into iOS Coredata. How would I make it JSON compatible with iOS ?

I know how to convert NSString to JSON object that will have Arrays of Dictionaries, but I don't know how would I put above given string as a NSString.

NSPratik
  • 4,714
  • 7
  • 51
  • 81
  • do you want it as a kind of NSString constant declared somewhere in your code? – heximal Apr 16 '16 at 10:56
  • So you want to put a JSON-formatted string into a CoreData database? If so, why? – trojanfoe Apr 16 '16 at 11:03
  • It is not clear what you mean. JSON is a way of serializing objects (converting them to text) for transport over the network. You usually convert your JSON data back to native objects once you receive them. To convert objects to/from JsON you use the methods in `NSJSONSerialization`. – Duncan C Apr 16 '16 at 11:04
  • NSString * dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; Check http://stackoverflow.com/questions/36617868/how-to-convert-json-to-nsarray/36618167#36618167 – Dmytro Shvetsov Apr 16 '16 at 11:38
  • I want to dump data while application launch for the first time. I will get data from postman by calling web-service and paste that JSON. So, I found that I can also add JSON file in bundle and read it when application launch for the first time. – NSPratik Apr 18 '16 at 05:58

2 Answers2

2

Your json is in the format of [Dictionary[Dictionary]]. So do the coding to make it in that format.

0

What I needed to just add a JSON type file in my App bundle and read it programatically.

Here is how I did it:

(1) Open TextEdit and create new document.

(2) In that empty document, PASTE the response of the web service. I used Postman to get the response. It was in JSON format.

(3) Click Format > Make Plain Text

(4) Save as: "yourFileName.json" enter image description here

(5) Now, just put this json file in your App bundle.

(6) Read content:

NSString  *filePath = [[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"json"];
NSData    *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];

NSError         *error = nil;
NSDictionary    *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSMutableArray  *arrUsers = jsonDict[kData];

(7) Enjoy :D

NSPratik
  • 4,714
  • 7
  • 51
  • 81