是否有可用於將.plist文件轉換爲JSON的命令行工具?將PLIST轉換爲JSON的命令行工具?
如果不是,那麼在Mac上使用Objective-C或C創建一個方法的方法是什麼?例如,對於Objective-C,有JSONKit。如何打開一個.plist文件,將它傳遞給JSONKit,並將其序列化爲JSON?
是否有可用於將.plist文件轉換爲JSON的命令行工具?將PLIST轉換爲JSON的命令行工具?
如果不是,那麼在Mac上使用Objective-C或C創建一個方法的方法是什麼?例如,對於Objective-C,有JSONKit。如何打開一個.plist文件,將它傳遞給JSONKit,並將其序列化爲JSON?
如果你是一個Mac上,你可以在命令行上plutil工具(這個自帶的開發者工具,我相信):作爲在評論中提到
plutil -convert json Data.plist
,這將覆蓋現有數據。要輸出到一個新的文件
plutil -convert json -o Data.json Data.plist
的代碼是相當簡單的做到這一點:
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個文件。
下就完事了—
// 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
。
我在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];
這將覆蓋原來的plist。爲了防止這個命令覆蓋原始文件,傳遞'-o'標誌。即'plutil -convert json Data.plist -o Data.json' – ADAM 2013-02-12 08:03:51
與此相關的一個複雜情況是一些Plist數據類型不能被plutil轉換。我發現的解決方法是在將plist傳遞給plutil之前對plist進行一些預處理。針對我正在使用的plist,我必須將''和''標籤替換爲''。 –
dandean
2013-05-15 05:01:18
我不知道這是應該工作的Mac,但在10。6它說:'未知的格式說明符:json' – Tobia 2013-05-25 10:31:40