1

I have a config file whose content is something like this:

main = {
delay = 10000;  
inputs = (
    {
        enabled = true;
        ip = "127.0.0.1"; 
        port = 10001; 
        file = "c:\abc.txt";
    },
    {
        enabled = true;
        ip = "127.0.0.1"; 
        port = 10002; 
        file = "c:\myfile.txt";
    },
);
}

Now, I want to parse this file, and for example, get the port number of the second input (i.e., 10002 in this example), etc.

Do you know what is the easiest way to do so in objective C?

Thanks!

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Blue Sky
  • 293
  • 1
  • 5
  • 14
  • convert JSON -> JSONKit or NSJSONSerialization – BLUEPIXY Dec 20 '13 at 17:37
  • That looks like the NSLog output (or `description`) of a `NSDictionary`, which is not suitable as config file format because it cannot reliably be parsed back (compare http://stackoverflow.com/questions/16783635/nsarray-to-description-and-vice-versa for a similar issue). - If possible, choose a different config file format (e.g. JSON). – Martin R Dec 20 '13 at 17:59

3 Answers3

1

Make sure it's a valid JSON file and then create a NSJSONSerialization object from the NSData of the file after opening it.

NSJSONSerialization *config = [[NSJSONSerialization JSONObjectWithData:DATAFROMFILE options:NSJSONReadingMutableContainers error:nil];

Then to access the second input port:

config[@"inputs"][1][@"port"]

But the best way to do this would be to create a model from each input so you could access the properties as strongly typed properties instead of by key.

ie. config.port instead of configInput[@"port"]
ApperleyA
  • 603
  • 4
  • 11
0

If you are able to change or modify the configuration file format to json or plist, you could simply use built in readers an parsers.

Else, there are third party approaches like libconfig.

Also this question may help.

Community
  • 1
  • 1
denolk
  • 765
  • 14
  • 27
0

Looks like your config contents were output by NSLog, which results in invalid JSON hence assuming that your actual config file is a valid JSON object, following code should get you what you need:

//Don't forget to replace "configfile" with your config file name in the project
NSString *configPath = [[NSBundle mainBundle] pathForResource:@"configfile" ofType:nil];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:configPath];
NSDictionary *config = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *ports = [config valueForKeyPath:@"main.inputs.port"];

//ports[0] is 10001
//ports[1] is 10002

Here you can verify if your JSON is valid: http://jsonlint.com. This is how your valid JSON confi should look like:

{
    "main": {
        "delay": "10000",
        "inputs": [
            {
                "enabled": true,
                "ip": "127.0.0.1",
                "port": 10001,
                "file": "c: \\abc.txt"
            },
            {
                "enabled": true,
                "ip": "127.0.0.1",
                "port": 10002,
                "file": "c: \\myfile.txt"
            }
        ]
    }
}

EDIT:

I would personally use a model framework rather than just a json parser to save you from a ton of manual work that comes with built-in NSJSONSerialization class. Here are couple of pretty good ones:

1) GitHub Mantle - https://github.com/MantleFramework/Mantle I use it where ever I can. It is very well written and thought out framework but has a little bit of learning curve involved, which probably is true to any new piece of software.

2) SBJson - https://github.com/stig/json-framework You can use SBJson if you just wanna get the job done, it has been pretty popular, especially before Mantle and other frameworks became available.

I hope it helps.

Yas Tabasam
  • 10,517
  • 9
  • 48
  • 53