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
다음은 작업을 완료합니다.
// 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
Reads in input.plist
, and writes the pretty printed JSON to output.json
.
The code is fairly simple to do this:
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];
I never got around to making it accept arguments as I only needed to do 3 files.
I wrote a tool in python to do this. See here:
http://sourceforge.net/projects/plist2json
Works from command line on os x or linux distros, batch converts a directory. It's short and simple so it should be easy to modify for your own purposes.
There is a native way, to convert plist
's to json
. It's called NSJSONSerialization.
Here is an example on how to use it, and convert a plist
file to a json
file:
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];
Using mac utils
Convert plist to json
plutil -convert json -o output.json input.plist
Convert json to plist
plutil -convert xml1 input.json -o output.plist
참고URL : https://stackoverflow.com/questions/6066350/command-line-tool-for-converting-plist-to-json
'developer tip' 카테고리의 다른 글
요소에 이벤트 리스너가 있는지 확인하십시오. (0) | 2020.12.11 |
---|---|
C에서 free와 malloc은 어떻게 작동합니까? (0) | 2020.12.10 |
자바 : Enum 대 Int (0) | 2020.12.10 |
줄 바꿈으로 분할하도록 IFS를 설정할 때 백 스페이스를 포함해야하는 이유는 무엇입니까? (0) | 2020.12.10 |
java.lang.RuntimeException : 처리기 (android.os.Handler)가 죽은 스레드에서 처리기로 메시지를 전송 (0) | 2020.12.10 |