【问题标题】:Parsing a config file解析配置文件
【发布时间】:2013-12-20 17:10:16
【问题描述】:

我有一个配置文件,其内容是这样的:

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";
    },
);
}

现在,我想解析这个文件,例如,获取第二个输入的端口号(即本例中的 10002)等。

你知道在目标 C 中最简单的方法是什么吗?

谢谢!

【问题讨论】:

  • 转换 JSON -> JSONKit 或 NSJSONSerialization
  • 这看起来像 NSDictionary 的 NSLog 输出(或 description),它不适合作为配置文件格式,因为它不能可靠地解析回来(比较 stackoverflow.com/questions/16783635/… 的类似问题)。 - 如果可能,选择不同的配置文件格式(例如 JSON)。

标签: objective-c parsing


【解决方案1】:

确保它是一个有效的 JSON 文件,然后在打开文件后从文件的 NSData 创建一个 NSJSONSerialization 对象。

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

然后访问第二个输入端口:

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

但最好的方法是从每个输入创建一个模型,这样您就可以将属性作为强类型属性而不是按键来访问。

ie. config.port instead of configInput[@"port"]

【讨论】:

    【解决方案2】:

    看起来您的配置内容是由 NSLog 输出的,这会导致 JSON 无效,因此假设您的实际配置文件是有效的 JSON 对象,以下代码应该可以满足您的需求:

    //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
    

    您可以在此处验证您的 JSON 是否有效:http://jsonlint.com。这就是您的有效 JSON 配置文件的样子:

    {
        "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"
                }
            ]
        }
    }
    

    编辑:

    我个人会使用模型框架而不是仅仅使用 json 解析器来让您免于内置 NSJSONSerialization 类带来的大量手动工作。这里有几个不错的:

    1) GitHub Mantle - https://github.com/MantleFramework/Mantle 我尽可能使用它。它的框架写得非常好,经过深思熟虑,但涉及到一点学习曲线,这可能适用于任何新软件。

    2) SBJson - https://github.com/stig/json-framework 如果您只想完成工作,可以使用 SBJson,它非常流行,尤其是在 Mantle 和其他框架可用之前。

    希望对你有帮助。

    【讨论】:

    • 谢谢,但是您知道任何免费且简单的用于目标 C 的 JSON 解析器吗?
    • @BlueSky: NSJSONSerialization 作为 Foundation 框架的一部分“内置”。
    • @MartinR :谢谢,我使用 gcc。我应该使用哪个头文件才能使用 NSJSONSerialization?
    • @BlueSky:你使用 Xcode 吗?只需@import <Foundation/Foundation.h>(通常已经在预编译的头文件中完成)。
    • @MartinR 不,我使用 MingW(Windows 下的 gcc)。现在,我应该包含什么文件?
    【解决方案3】:

    如果您能够将配置文件格式更改或修改为jsonplist,您可以简单地使用内置阅读器和解析器。

    另外,还有像libconfig 这样的第三方方法。

    this question 也可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2021-08-14
      • 2016-03-12
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2012-08-31
      • 2012-02-16
      • 1970-01-01
      相关资源
      最近更新 更多