【发布时间】:2011-05-20 00:39:11
【问题描述】:
是否有可用于将 .plist 文件转换为 JSON 的命令行工具?
如果不是,在 Mac 上使用 Objective-C 或 C 创建一个的方法是什么?例如,有用于 Objective-C 的 JSONKit。如何打开一个 .plist 文件,将其传递给 JSONKit,然后将其序列化为 JSON?
【问题讨论】:
标签: objective-c json plist
是否有可用于将 .plist 文件转换为 JSON 的命令行工具?
如果不是,在 Mac 上使用 Objective-C 或 C 创建一个的方法是什么?例如,有用于 Objective-C 的 JSONKit。如何打开一个 .plist 文件,将其传递给 JSONKit,然后将其序列化为 JSON?
【问题讨论】:
标签: objective-c json plist
如果你在 Mac 上,你可以在命令行上使用 plutil 工具(我相信这是开发者工具自带的):
plutil -convert json Data.plist
如 cmets 中所述,这将覆盖现有数据。输出到新文件
plutil -convert json -o Data.json Data.plist
【讨论】:
-o 标志。即plutil -convert json Data.plist -o Data.json
<string> 替换 <data> 和 <date> 标签。
plutil -convert json -o Data.json Data.plist。也就是说,原来的plist文件应该是最后了。 @亚当
plutil -convert -r json 接收人类可读格式的json
plutil -convert json -o - file.json
下面的工作完成了——
// convertPlistToJSON.m
#import <Foundation/Foundation.h>
#import "JSONKit.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(argc != 3) { fprintf(stderr, "usage: %s FILE_PLIST FILE_JSON\n", argv[0]); exit(5); }
NSString *plistFileNameString = [NSString stringWithUTF8String:argv[1]];
NSString *jsonFileNameString = [NSString stringWithUTF8String:argv[2]];
NSError *error = NULL;
NSData *plistFileData = [NSData dataWithContentsOfFile:plistFileNameString options:0UL error:&error];
if(plistFileData == NULL) {
NSLog(@"Unable to read plist file. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
id plist = [NSPropertyListSerialization propertyListWithData:plistFileData options:NSPropertyListImmutable format:NULL error:&error];
if(plist == NULL) {
NSLog(@"Unable to deserialize property list. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
NSData *jsonData = [plist JSONDataWithOptions:JKSerializeOptionPretty error:&error];
if(jsonData == NULL) {
NSLog(@"Unable to serialize plist to JSON. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
if([jsonData writeToFile:jsonFileNameString options:NSDataWritingAtomic error:&error] == NO) {
NSLog(@"Unable to write JSON to file. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
[pool release]; pool = NULL;
return(0);
}
它会进行一些合理的错误检查,但它不是防弹的。使用风险自负。
您需要JSONKit 来构建该工具。将JSONKit.m和JSONKit.h与convertPlistToJSON.m放在同一目录下,然后编译:
shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
用法:
shell% convertPlistTOJSON
usage: convertPlistToJSON FILE_PLIST FILE_JSON
shell% convertPlistTOJSON input.plist output.json
读入input.plist,并将打印出来的漂亮JSON写入output.json。
【讨论】:
执行此操作的代码相当简单:
NSArray* array = [[NSArray arrayWithContentsOfFile:[@"~/input.plist" stringByExpandingTildeInPath]]retain];
SBJsonWriter* writer = [[SBJsonWriter alloc] init];
NSString* s = [[writer stringWithObject:array] retain];
[s writeToFile:[@"~/output.json" stringByExpandingTildeInPath] atomically:YES];
[array release];
我从来没有让它接受参数,因为我只需要做 3 个文件。
【讨论】:
我在 python 中编写了一个工具来执行此操作。见这里:
http://sourceforge.net/projects/plist2json
在 os x 或 linux 发行版上从命令行工作,批量转换目录。它简短而简单,因此应该很容易根据您自己的目的进行修改。
【讨论】:
有一种本地方式,将plist 转换为json。它叫做NSJSONSerialization。
这是一个关于如何使用它的示例,并将plist 文件转换为json 文件:
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:@"input.plist"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plistDict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[jsonString writeToFile:@"output.json" atomically:NO encoding:NSUTF8StringEncoding error:&error];
【讨论】:
使用 mac 工具
plutil -convert json -o output.json input.plist
plutil -convert xml1 input.json -o output.plist
【讨论】:
将Filename.plist 转换为Filename.json:
plutil -convert json -r -e json Filename.plist
-convert 表示格式,-r 使输出更易于阅读,-e 指定扩展名
【讨论】: