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.