-1

I have a file-"xyz.txt" in my applicaion bundle with just JSON formatted text. I want to open the filepath of the text file and read the content to NSDictionary. How to do that. I already have my JSON string ready. Just need to initliaze to NSdictionary. If I directly initilize in code , i would need lot of @ before evey key and value. and I want to avoid that. That is the reason , i put JSON in a file-"xyz.txt"

I have two ways - 1.I read form file and assign it using [[NSDictionary alloc] initWithContentsOfFile:filePath]; 2.I assign the string to NSDictionay with lot of @. I want to avoid using @ all the time

I know we can do via NSData id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

but even in that case we will need to initialize string to NSData with @ for all key and value.

Any solution that can read the json from a text file and output assign to a NSDictionary

{
"ABC": [
    {
      "id": "01",
      "score": "0.2"
    },
    {
      "id": "02",
      "score": "0.2"
    }
  ],
  "XYZ": [
    {
      "id": "01",
      "score": "0.9"
    },
    {
      "id": "02",
      "score": "0.9"
    }
   ],
  "PQR": [
    {
      "id": "01",
      "score": "0.9"
    },
    {
      "id": "02",
      "score": "0.3"
    },
    {
      "id": "03",
      "score": "0.2"
    }
   ]
}
  • possible duplicate of [How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)](http://stackoverflow.com/questions/8606444/how-do-i-deserialize-a-json-string-into-an-nsdictionary-for-ios-5) – Renfei Song May 31 '15 at 11:45
  • [[NSDictionary alloc] initWithContentsOfFile:filePath]; using above I can initialise an NSDictionary, but how to do it , any examples. Because , I already have my JSON string ready. Just need to initliaze to NSdictionary. If I directly initilize in code , i would need lot of @ before evey key and value. and I want to avoid that. That is the reason , i put JSON in a file – Sunil Panda May 31 '15 at 11:47
  • first search for how to load file data, then search for how to convert the JSON data into an object graph – Wain May 31 '15 at 11:52

2 Answers2

4

JSON in string stored in file -> NSDictionary

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyDict" ofType:@"text"];
NSData * myDictData = [NSData dataWithContentsOfFile:filePath];
NSError *error;
NSDictionary * myDictionary = [NSJSONSerialization JSONObjectWithData:myDictData
                                                     options:NSJSONReadingMutableContainers
                                                       error:&error];

if (error != nil)
{
    NSLog(@"Error parsing JSON:\n%@",error.userInfo);
    return;
}
1

Get the file's data using NSData's dataWithContentsOfFile method (the file path should be the path to the file in the bundle; there is a method that can get you the path of a resource from the app main bundle).

Then use NSJSONSerialization's JSONObjectWithData method to create a NSDictionary from the JSON data.

Artal
  • 8,933
  • 2
  • 27
  • 30
  • Yes , I was looking for something like reading strings as a whole from a file. thanks – Sunil Panda May 31 '15 at 12:28
  • its actually quite handy when you want to keep some static data in the iOS app and read it later during runtime. we can use read from file. I dont understand why same cant be done for NSDictionary – Sunil Panda May 31 '15 at 12:30
  • @SunilPanda I agree that it can be useful. So you were able to achieve your goal by loading the file using NSData? – Artal May 31 '15 at 12:35